zthread学习 实例十 线程间的协助(一)

使用线程在同一时刻运行多个任务时,可以使用互斥锁来同步多个任务,阻止一个任务干扰另一个任务的资源。也就是说,使用互斥锁来保证同一时刻只允许一个任务访问共享资源。

 

 

线程间协作:多个线程一起工作来共同解决某个问题(问题的某一部分必须在另一部分被解决之前解决完毕)。

 

 

ZThread库中使用 互斥锁 并允许 挂起 的基类Condition,可以通过wait()挂起一个任务(不是忙等待),当外部状态发生改变时,通过signal()或broadcast()来唤醒这个任务。

 

wait()的工作方式:它会释放被Condition对象所控制的互斥锁Mutex(很关键),并且挂起线程。正因为会释放被Condition对象所控制的互斥锁Mutex,这就意味着该Mutex可以被其他线程获得,使当前线程挂起(等待其他线程的某个作务完成),并把运行控制权交给其他线程。

 

 

下面是一个关于线程协助的简单例子:一个线程给Car上蜡,一个线程给Car抛光。抛光线程在上蜡完成之前不能进行工作、上蜡线程在Car可以再上蜡外套之前必须等到抛光线程完成:

[cpp]  view plain copy
  1. #include <vld.h>  
  2. #include "stdafx.h"  
  3. #include "zthread/Mutex.h"  
  4. #include "zthread/Guard.h"  
  5. #include "zthread/Condition.h"  
  6. #include "zthread/CountedPtr.h"  
  7. #include <iostream>  
  8. using namespace ZThread;  
  9. using namespace std;  
  10. class Car  
  11. {  
  12. public:  
  13.     Car(): condition(Lock), bIsWaxon(false){}  
  14.     void Waxed()        //上蜡  
  15.     {  
  16.         Guard<Mutex> g(Lock);  
  17.         bIsWaxon = true;  
  18.         condition.signal();     //唤醒等待condition的线程  
  19.     }  
  20.     void Buffed()           //抛光  
  21.     {  
  22.         Guard<Mutex> g(Lock);  
  23.         bIsWaxon = false;  
  24.         condition.signal();     //唤醒等待condition的线程  
  25.     }  
  26.       
  27.       
  28.     //  
  29.     void WaitforWaxing()    //等待上蜡  
  30.     {  
  31.         //这个保护锁很特别,它会在wait()调用时释放掉。  
  32.         Guard<Mutex> g(Lock);  
  33.         while (bIsWaxon == false)  
  34.             condition.wait();  
  35.     }  
  36.     void WaitforBuffing()   //等待抛光  
  37.     {  
  38.         Guard<Mutex> g(Lock);  
  39.         while(bIsWaxon == true)  
  40.             condition.wait();  
  41.     }  
  42. private:  
  43.     Mutex Lock;  
  44.     Condition condition;        //用于线程间协作  
  45.     bool    bIsWaxon;           //是否上蜡  
  46. };  
  47. //上蜡任务  
  48. class  WaxOn : public Runnable  
  49. {  
  50. public:  
  51.     WaxOn(CountedPtr<Car>& ACar): pCar(ACar){}  
  52.     void run()  
  53.     {  
  54.         try  
  55.         {  
  56.             while (!Thread::interrupted())  
  57.             {  
  58.                 cout << " Wax On! " <<endl;  
  59.                 Thread::sleep(200);  
  60.                 pCar->Waxed();  
  61.                 pCar->WaitforBuffing();      //上完蜡之后如果再想上蜡,必须等待抛光之后,挂起线程  
  62.             }  
  63.         }  
  64.         catch (Interrupted_Exception& e)  
  65.         {  
  66.             cerr << " Jarry Wax On : " << e.what() <<endl;  
  67.         }  
  68.     }  
  69. private:  
  70.     CountedPtr<Car> pCar;  
  71. };  
  72. //抛光任务  
  73. class WaxOff : public Runnable  
  74. {  
  75. public:  
  76.     WaxOff(CountedPtr<Car>& ACar) : pCar(ACar){}  
  77.     void run()  
  78.     {  
  79.         try  
  80.         {  
  81.             while (!Thread::interrupted())  
  82.             {  
  83.                 pCar->WaitforWaxing();       //抛光之前必须先完成上蜡操作  
  84.                 Thread::sleep(200);  
  85.                 cout <<" Wax Off !" <<endl;  
  86.                 pCar->Buffed();  
  87.             }  
  88.         }  
  89.         catch (Interrupted_Exception& e)  
  90.         {  
  91.             cerr << "Jarry Wax Off : " << e.what() <<endl;  
  92.         }  
  93.     }  
  94. private:  
  95.     CountedPtr<Car> pCar;  
  96. };  
  97. int main()  
  98. {  
  99.     try  
  100.     {  
  101.         CountedPtr<Car> car(new Car);  
  102.         ThreadedExecutor executor;  
  103.         executor.execute(new WaxOff(car));  
  104.         executor.execute(new WaxOn(car));  
  105.         cin.get();  
  106.         executor.interrupt();       //执行器的中断,会为其控制下的所有线程调用interrupt()  
  107.         cin.get();  
  108.     }  
  109.     catch(Synchronization_Exception& e)  
  110.     {  
  111.         cerr << "Jarry Main :" <<e.what() <<endl;  
  112.     }  
  113.     return 0;  
  114. }  
 

代码中,wait()的调用被置于一个while循环中,用这个循环来检查相关条件。主要基于两个原因:

1、很可能当某个线程得一个信号signal()时,其他一些条件可能已经改变了,但这些条件在这里与调用wait()的原因无关。如果有这种情况,该线程在其相关的条件改变这前将再一次被挂起。

2、在该线程从其wait()函数中醒来之时,可能另外某个任务改变了一些条件,因此这个线程就不能或者没兴趣在此时执行其操作了。于是再次调用wait()重新挂起。

——基于以上这两个原因在调用wait()时总会出现,故总是将wait()置于while循环中来测试与线程相关的条件(如本例中的 bool bIsWaxon)。

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值