zthread学习 实例十 线程间的协助(四)——读者、写者

对于读写问题,ZThread库专门封装了类(ReadWriteLock)来控制读写:

zthread学习 实例十 线程间的协助(四)——读者、写者_第1张图片

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学习 实例十 线程间的协助(四)——读者、写者_第2张图片 

你可能感兴趣的:(thread,exception,String,OS,Access,output)