zthread学习 实例十 线程间的协助(四)——读者、写者
2011-06-01 14:44
369 查看
对于读写问题,ZThread库专门封装了类(ReadWriteLock)来控制读写:
FairReadWriteLock (按FIFO的顺序进行读写控制):
A FairReadWriteLock maintains a balance between the order read-only access and read-write access is allowed. Threads contending for the pair of Lockable objects this ReadWriteLock provides will gain access to the locks in FIFO order.
BiasedReadWriteLock(写优先的读写控制):
A BiasedReadWriteLock has a bias toward writers. It will prefer read-write access over read-only access when many threads are contending for access to either Lockable this ReadWriteLock provides.
下面是试测试代码(文中带有注释,因此后面不再分析程序):
使用FairReadWriteLock时的运行结果: 使用BiasedReadWriteLock时的运行结果:
FairReadWriteLock (按FIFO的顺序进行读写控制):
A FairReadWriteLock maintains a balance between the order read-only access and read-write access is allowed. Threads contending for the pair of Lockable objects this ReadWriteLock provides will gain access to the locks in FIFO order.
BiasedReadWriteLock(写优先的读写控制):
A BiasedReadWriteLock has a bias toward writers. It will prefer read-write access over read-only access when many threads are contending for access to either Lockable this ReadWriteLock provides.
下面是试测试代码(文中带有注释,因此后面不再分析程序):
#include "stdafx.h" #include "zthread/Runnable.h" #include "zthread/Thread.h" #include "zthread/CountedPtr.h" #include "zthread/ReadWriteLock.h" #include "zthread/FairReadWriteLock.h" #include "zthread/BiasedReadWriteLock.h" #include "Display.h" using namespace std; using namespace ZThread; class ReadThread : public Runnable { public: ReadThread(CountedPtr<ReadWriteLock>& lock, CountedPtr<Display>& disp, int idn = -1) : Lock(lock),display(disp), id(idn){} void run() { try { while (!Thread::interrupted()) { //获得 读 互斥锁 Lock->getReadLock().acquire(); //模拟 读操作 output(" Reading..."); Thread::sleep(100); //释放 读 互斥锁 Lock->getReadLock().release(); //特意留出空闲时间,使线程能根据BiasedReadWriteLock的行为调度线程 Thread::sleep(500); } } catch (Interrupted_Exception& e) { output(e.what()); } } //输出 void output(string str) { ostringstream os; os << *this << " : " << str <<endl; display->OutPut(os); } friend ostream& operator<< (ostream& os, const ReadThread& readThread) { return os << readThread.id; } private: //共享的读写控制锁,且 写操作 优先于 读操作 CountedPtr<ReadWriteLock> Lock; CountedPtr<Display> display; int id; }; class WriteThread : public Runnable { public: WriteThread(CountedPtr<ReadWriteLock>& lock, CountedPtr<Display>& disp, int idn = -1) : Lock(lock), display(disp), id(idn){} void run() { try { while (!Thread::interrupted()) { //获得 写 互斥锁 Lock->getWriteLock().acquire(); //模拟 写操作 output(" Wirtting..."); Thread::sleep(100); //释放 写 互斥锁 Lock->getWriteLock().release(); //特意留出空闲时间,使线程能根据BiasedReadWriteLock的行为调度线程 Thread::sleep(500); } } catch (Interrupted_Exception& e) { output(e.what()); } } //同步输出 void output(string str) { ostringstream os; os << *this << " : " << str <<endl; display->OutPut(os); } friend ostream& operator<< (ostream& os, const WriteThread& writeThread) { return os << writeThread.id; } private: CountedPtr<ReadWriteLock> Lock; CountedPtr<Display> display; int id; }; int main() { try { //FairReadWriteLock是FIFO顺序的读写控制锁 CountedPtr<ReadWriteLock> ReadWrite( new FairReadWriteLock); //BiasedReadWriteLock是写优先的读写控制锁 //CountedPtr<ReadWriteLock> ReadWrite( new BiasedReadWriteLock); CountedPtr<Display> disp(new Display); ThreadedExecutor executor; for (int i = 0; i < 5; i++) { executor.execute(new ReadThread(ReadWrite, disp, i)); } for (int i = 0; i < 5; i++) { executor.execute(new WriteThread(ReadWrite, disp, i)); } cin.get(); executor.interrupt(); cin.get(); } catch (Synchronization_Exception& e) { cerr << "Jarry Main Exception " << e.what() << endl; } }
使用FairReadWriteLock时的运行结果: 使用BiasedReadWriteLock时的运行结果:
相关文章推荐
- zthread学习 实例十三 线程间的协助(四)——读者、写者
- zthread学习 实例十 线程间的协助(一)
- zthread学习 实例十 线程间的协助(二)
- zthread学习 实例十二 线程间的协助(三)——死锁
- zthread学习 实例十一 线程间的协助(二)
- zthread学习 实例十 线程间的协助(三)——死锁
- zthread学习 实例十 线程间的协助(一)
- zthread学习 实例七 线程本地存储
- zthread学习 实例三 使用Executor器来执行线程
- zthread学习 实例七 线程本地存储
- 12.[个人]C++线程入门到进阶(12)----读者写者问题&读写锁SRWLock
- zthread学习 实例二
- zthread学习 实例十一 信号量机制
- zthread学习 实例五 非任务对象的生存期
- TCP-IP学习笔记三:NIO的网络编程-单线程实例
- Java进程间的同步与互斥实例(实现读者写者问题)
- 【Linux学习笔记】39:Linux下C模拟读者写者问题
- 学习笔记--线程的几个常用方法实例
- zthread学习 实例十二 ZThread库基本结构的规纳
- 进程、线程知识点总结和同步(消费者生产者,读者写者三类问题)、互斥、异步、并发、并行、死锁、活锁的总结