C++代码  C++ boost thread学习(一)
  1. #include <boost/thread.hpp>  
  2. #include <boost/thread/mutex.hpp>  
  3.   
  4. #include <iostream>  
  5.   
  6. boost::mutex io_mutex;  
  7.   
  8. using namespace std;  
  9.   
  10. void wait(int seconds)  
  11. {  
  12. boost::this_thread::sleep(boost::posix_time::seconds(seconds));  
  13. }  
  14.   
  15. void interruptedThread()  
  16. {  
  17.     try  
  18.     {  
  19.         for (int i = 0; i < 5; i++)  
  20.         {  
  21.             wait(1);  
  22.             cout << i << endl;  
  23.         }  
  24.     }  
  25.     catch (boost::thread_interrupted&)  
  26.     {  
  27.         cout << "thread_interrupted exception happened";  
  28.     }  
  29. }  
  30.   
  31. void testInteruptedThread()  
  32. {  
  33.     boost::thread t(interruptedThread);  
  34.     wait(3);  
  35.     t.interrupt();  
  36.     t.join();  
  37. }  
  38.   
  39. int main(int argc, char* argv[])  
  40. {  
  41.     testInteruptedThread();  
  42.     return 0;  
  43. }  

相关文章: