zthread学习 实例十四 信号量机制

来源:互联网 发布:手机编程前景 编辑:程序博客网 时间:2024/04/28 17:11

ZThread库提供了另外一种同步机制——信号量,它定义了两种信号量:Semaphore(带上界) 和 CountingSemaphore(不带上界),都类似于传统的信号量,下面例子说明了其用法:

[cpp] view plaincopy
  1. #include "stdafx.h"  
  2. #include "Display.h"  
  3. #include <iostream>  
  4. using namespace std;  
  5. using namespace ZThread;  
  6. class Reads : public Runnable  
  7. {  
  8. public:  
  9.     Reads(CountedPtr<Semaphore>/*CountedPtr<CountingSemaphore>*/& aSemaphore, CountedPtr<Display>& disp, int idn = -1):  
  10.     id(idn), pSemaphore(aSemaphore), pDisplay(disp){}  
  11.       
  12.     void run()  
  13.     {  
  14.         try  
  15.         {  
  16.             while (!Thread::interrupted())  
  17.             {  
  18.                 //信号量减1  
  19.                 pSemaphore->acquire();  
  20.                   
  21.                 output(" ,Jarry Reading...");  
  22.                 Thread::sleep(1000);  
  23.                   
  24.                 //信号量增1  
  25.                 pSemaphore->release();  
  26.             }  
  27.         }  
  28.         catch (Interrupted_Exception& e)  
  29.         {  
  30.             cerr << "Jarry " << e.what() << endl;  
  31.         }  
  32.         catch (InvalidOp_Exception& e)      //越界,结束该线程  
  33.         {  
  34.             cerr << "Jarry " << e.what() << endl;  
  35.             return;  
  36.         }  
  37.     }  
  38.     //同步输出  
  39.     void output(string str)  
  40.     {  
  41.         ostringstream os;  
  42.         os << *this << " : " << str <<endl;  
  43.         pDisplay->OutPut(os);  
  44.     }  
  45.     friend ostream& operator<< (ostream& os, Reads& reads)  
  46.     {  
  47.         return os << "id: " << reads.id << " Count = " << reads.pSemaphore->count();  
  48.     }  
  49. private:  
  50.     int         id;  
  51.     CountedPtr<Semaphore> pSemaphore;  
  52.     //CountedPtr<CountingSemaphore> pSemaphore;  
  53.     CountedPtr<Display> pDisplay;  
  54. };  
  55.   
  56. int main()  
  57. {  
  58.     //创建一个count为5信号量,当count计数达到上界时,就会抛出InvalidOp_Exception异常  
  59.     CountedPtr<Semaphore> pSemaphore(new Semaphore(5, 2));  
  60.     //类似Semaphore,只是没有一个上界,不会抛出InvalidOp_Exception异常  
  61.     //CountedPtr<CountingSemaphore>pSemaphore(new CountingSemaphore(5));  
  62.       
  63.     CountedPtr<Display> display(new Display);  
  64.     ThreadedExecutor executor;  
  65.       
  66.     for (int i = 0; i < 20; i ++)  
  67.     {  
  68.         //这样测试是为了有在创建线程之前有线程执行,并且能促使信号量增1  
  69.         executor.wait(1500);  
  70.           
  71.         executor.execute(new Reads(pSemaphore, display, i));  
  72.     }  
  73.       
  74.     cin.get();  
  75.     executor.interrupt();  
  76.     cin.get();  
  77.     return 0;  
  78. }  
 

运行结果:

 

其中同步显示类,好像还没有贴出来,暂且贴在些吧:

[cpp] view plaincopy
  1. #ifndef DISPLAY_H  
  2. #define DISPLAY_H  
  3. #include <iostream>  
  4. #include <sstream>  
  5. using namespace ZThread;  
  6. using namespace std;  
  7. class Display  
  8. {  
  9. public:  
  10.     void OutPut(ostringstream& os)  
  11.     {  
  12.         Guard<Mutex> g(Lock);  
  13.         cout<<os.str();  
  14.     }  
  15. private:  
  16.     Mutex Lock;   
  17. };  
  18. #endif  
 

0 0
原创粉丝点击