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

对于读写问题,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.

 

下面是试测试代码(文中带有注释,因此后面不再分析程序):

[cpp]  view plain copy
  1. #include "stdafx.h"  
  2. #include "zthread/Runnable.h"  
  3. #include "zthread/Thread.h"  
  4. #include "zthread/CountedPtr.h"  
  5. #include "zthread/ReadWriteLock.h"  
  6. #include "zthread/FairReadWriteLock.h"  
  7. #include "zthread/BiasedReadWriteLock.h"  
  8. #include "Display.h"  
  9. using namespace std;  
  10. using namespace ZThread;  
  11. class ReadThread : public Runnable  
  12. {  
  13. public:  
  14.     ReadThread(CountedPtr<ReadWriteLock>& lock, CountedPtr<Display>& disp, int idn = -1) :  
  15.     Lock(lock),display(disp), id(idn){}  
  16.       
  17.     void run()  
  18.     {  
  19.         try  
  20.         {  
  21.             while (!Thread::interrupted())  
  22.             {  
  23.                 //获得 读 互斥锁  
  24.                 Lock->getReadLock().acquire();  
  25.                   
  26.                 //模拟 读操作  
  27.                 output(" Reading...");  
  28.                 Thread::sleep(100);  
  29.                   
  30.                 //释放 读 互斥锁  
  31.                 Lock->getReadLock().release();  
  32.                   
  33.                 //特意留出空闲时间,使线程能根据BiasedReadWriteLock的行为调度线程  
  34.                 Thread::sleep(500);  
  35.             }  
  36.         }  
  37.         catch (Interrupted_Exception& e)  
  38.         {  
  39.             output(e.what());  
  40.         }  
  41.     }  
  42.       
  43.     //输出  
  44.     void output(string str)  
  45.     {  
  46.         ostringstream os;  
  47.         os << *this << " : " << str <<endl;  
  48.         display->OutPut(os);  
  49.     }  
  50.     friend ostream& operator<< (ostream& os, const ReadThread& readThread)  
  51.     {  
  52.         return os << readThread.id;  
  53.     }  
  54. private:  
  55.     //共享的读写控制锁,且 写操作 优先于 读操作  
  56.     CountedPtr<ReadWriteLock> Lock;     
  57.     CountedPtr<Display>   display;  
  58.     int id;  
  59. };  
  60. class WriteThread : public Runnable  
  61. {  
  62. public:  
  63.     WriteThread(CountedPtr<ReadWriteLock>& lock, CountedPtr<Display>& disp, int idn = -1) :  
  64.      Lock(lock), display(disp), id(idn){}  
  65.       
  66.     void run()  
  67.     {  
  68.         try  
  69.         {  
  70.             while (!Thread::interrupted())  
  71.             {  
  72.                 //获得 写 互斥锁  
  73.                 Lock->getWriteLock().acquire();  
  74.                   
  75.                 //模拟 写操作  
  76.                 output(" Wirtting...");  
  77.                 Thread::sleep(100);  
  78.                   
  79.                 //释放 写 互斥锁  
  80.                 Lock->getWriteLock().release();  
  81.                   
  82.                 //特意留出空闲时间,使线程能根据BiasedReadWriteLock的行为调度线程  
  83.                 Thread::sleep(500);  
  84.                   
  85.             }  
  86.         }  
  87.         catch (Interrupted_Exception& e)  
  88.         {  
  89.             output(e.what());  
  90.         }  
  91.     }  
  92.       
  93.     //同步输出  
  94.     void output(string str)  
  95.     {  
  96.         ostringstream os;  
  97.         os << *this << " : " << str <<endl;  
  98.         display->OutPut(os);  
  99.     }  
  100.     friend ostream& operator<< (ostream& os, const WriteThread& writeThread)  
  101.     {  
  102.         return os << writeThread.id;  
  103.     }  
  104. private:  
  105.     CountedPtr<ReadWriteLock> Lock;  
  106.     CountedPtr<Display>   display;  
  107.     int id;  
  108. };  
  109. int main()  
  110. {  
  111.     try  
  112.     {  
  113.         //FairReadWriteLock是FIFO顺序的读写控制锁  
  114.         CountedPtr<ReadWriteLock> ReadWrite( new FairReadWriteLock);  
  115.           
  116.         //BiasedReadWriteLock是写优先的读写控制锁  
  117.         //CountedPtr<ReadWriteLock> ReadWrite( new BiasedReadWriteLock);  
  118.               
  119.         CountedPtr<Display> disp(new Display);  
  120.           
  121.         ThreadedExecutor executor;  
  122.           
  123.           
  124.         for (int i = 0; i < 5; i++)  
  125.         {  
  126.             executor.execute(new ReadThread(ReadWrite, disp, i));  
  127.         }  
  128.           
  129.         for (int i = 0; i < 5; i++)  
  130.         {  
  131.             executor.execute(new WriteThread(ReadWrite, disp, i));  
  132.         }  
  133.           
  134.         cin.get();  
  135.         executor.interrupt();  
  136.         cin.get();  
  137.     }  
  138.     catch (Synchronization_Exception& e)  
  139.     {  
  140.         cerr << "Jarry Main Exception " << e.what() << endl;  
  141.     }  
  142. }  
 

 

使用FairReadWriteLock时的运行结果: 使用BiasedReadWriteLock时的运行结果:

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值