boost库中thread多线程详解5——谈谈线程中断

线程不是在任意时刻都可以被中断的。如果将线程中函数中的sleep()睡眠等待去掉,那么即使在主线程中调用interrupt()线程也不会被中断。
thread库预定义了若干个线程的中断点,只有当线程执行到中断点的时候才能被中断,一个线程可以拥有任意多个中断点。

thread库预定义了共9个中断点,它们都是函数,如下:

1. thread::join();
2. thread::timed_join();
3. condition_variable::wait();
4. condition_variable::timed_wait();
5. condition_variable_any::wait();
6. condition_variable_any::timed_wait();
7. thread::sleep();
8. this_thread::sleep();
9. this_thread::interruption_point()
这些中断点中的前8个都是某种形式的等待函数,表明线程在阻塞等待的时候可以被中断。

而最后一个位于子名字空间this_thread的interruption_point()则是一个特殊的中断点函数,它并不等待,只是起到一个标签的作用,表示线程执行到这个函数所在的语句就可以被中断。看看下面的例子:

[cpp]  view plain  copy
 print ?
  1. namespace  
  2. {  
  3.     boost::mutex io_mu;  
  4.   
  5.     void to_interrupt(const std::string& str)  
  6.     {  
  7.         // 如果在线程外部调用了this_thread->interrupt()  
  8.         // 线程内部的以下这些检查点可以抛出boost::thread_interrupted异常  
  9.         try  
  10.         {  
  11.             boost::this_thread::disable_interruption();  
  12.             for (int i = 0; i < 5; ++i)  
  13.             {  
  14.                 boost::mutex::scoped_lock lock(io_mu);  
  15.                 PRINT_DEBUG(i);  
  16.                 PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());  
  17.                 // PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_requested());  
  18.                 if (i == 2)  
  19.                 {  
  20.                     PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());  
  21.                     boost::this_thread::interruption_point();  
  22.                     PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());  
  23.                 }  
  24.             }  
  25.         }  
  26.         catch (boost::thread_interrupted &)  
  27.         {  
  28.         }  
  29.     }      
  30. }  
  31.   
  32. void test_thread_interrupt()  
  33. {  
  34.     boost::thread t(to_interrupt, "hello");  
  35.     // 中断函数  
  36.     t.interrupt();  
  37.     t.join();  
  38. }  
运行结果:
[cpp]  view plain  copy
 print ?
  1. 2013-01-02 11:00:44 263 [8272] DEBUG - 0  
  2. 2013-01-02 11:00:44 266 [8272] DEBUG - 1  
  3. 2013-01-02 11:00:44 269 [8272] DEBUG - 2  
如果注释boost::this_thread::interrupt_point了,则结果如下:
[cpp]  view plain  copy
 print ?
  1. 2013-01-02 11:02:06 555 [5168] DEBUG - 0  
  2. 2013-01-02 11:02:06 559 [5168] DEBUG - 1  
  3. 2013-01-02 11:02:06 561 [5168] DEBUG - 2  
  4. 2013-01-02 11:02:06 564 [5168] DEBUG - 3  
  5. 2013-01-02 11:02:06 567 [5168] DEBUG - 4  
下面谈谈启用/禁用线程中断
缺省情况下钱程都是允许中断的,但thread库允许控制线程的中断行为。
thread 库在子名字空间this_thread提供了一组函数和类来共同完成线程的中断启用和禁用:
1. interruption_enabled(): 函数检测当前线程是否允许中断
2. interruption_requested(): 函数检测当前线程是否被要求中断
3. 类disable_interruption是一个RAII类型的对象,它在构造时关闭线程的中断,析构时自动恢复线程的中断状态。在disable_interruption 的生命期内线程始终是不可中断的,除非使用了restore_interruption 对象。
4. restore_interruption只能在disable_interruption 的作用域内使用,它在构造时临时打开线程的中断状态,在析构时又关闭中断状态。
这些中断点中的前8个都是某种形式的等待函数,表明线程在阻塞等待的时候可以被中断。

[cpp]  view plain  copy
 print ?
  1. namespace  
  2. {  
  3.     boost::mutex io_mu;  
  4.   
  5.     void to_interrupt_disable(const std::string& str)  
  6.     {  
  7.         // 默认可以中断  
  8.         assert(boost::this_thread::interruption_enabled());  
  9.           
  10.         for (int i = 0; i < 10; i++)  
  11.         {  
  12.             // 关闭中断  
  13.             boost::this_thread::disable_interruption di;            
  14.             // 此时中断不可用  
  15.             PRINT_DEBUG(std::boolalpha << "interruption_enabled = " <<  boost::this_thread::interruption_enabled());  
  16.             // 是否有中断请求  
  17.             PRINT_DEBUG(std::boolalpha << "interruption_requested = " <<  boost::this_thread::interruption_requested());  
  18.   
  19.             boost::mutex::scoped_lock lock(io_mu);  
  20.             PRINT_DEBUG(i);  
  21.             // 使用中断点函数,因为关闭中断,此时无效果。 中断恢复后,它才生效。  
  22.             boost::this_thread::interruption_point();  
  23.   
  24.             if (i == 8)  
  25.             {  
  26.                 // 临时恢复中断  
  27.                 boost::this_thread::restore_interruption ri(di);  
  28.                 PRINT_DEBUG(std::boolalpha << "interruption_enabled = " <<  boost::this_thread::interruption_enabled());  
  29.                 PRINT_DEBUG(std::boolalpha << "interruption_enabled after restore = " <<  boost::this_thread::interruption_enabled());  
  30.                 boost::this_thread::interruption_point();  
  31.             }  
  32.         }  
  33.     }  
  34. }  
  35.   
  36. void test_thread_interrupt_disable()  
  37. {  
  38.     boost::thread t(to_interrupt_disable, "hello");  
  39.     t.interrupt();  
  40.     t.join();  
  41. }  
结果:
[cpp]  view plain  copy
 print ?
  1. 2013-01-02 14:09:35 538 [7628] DEBUG - interruption_enabled = false  
  2. 2013-01-02 14:09:35 544 [7628] DEBUG - interruption_requested = true  
  3. 2013-01-02 14:09:35 551 [7628] DEBUG - 0  
  4. 2013-01-02 14:09:35 555 [7628] DEBUG - interruption_enabled = false  
  5. 2013-01-02 14:09:35 563 [7628] DEBUG - interruption_requested = true  
  6. 2013-01-02 14:09:35 570 [7628] DEBUG - 1  
  7. 2013-01-02 14:09:35 574 [7628] DEBUG - interruption_enabled = false  
  8. 2013-01-02 14:09:35 581 [7628] DEBUG - interruption_requested = true  
  9. 2013-01-02 14:09:35 586 [7628] DEBUG - 2  
  10. 2013-01-02 14:09:35 589 [7628] DEBUG - interruption_enabled = false  
  11. 2013-01-02 14:09:35 601 [7628] DEBUG - interruption_requested = true  
  12. 2013-01-02 14:09:35 608 [7628] DEBUG - 3  
  13. 2013-01-02 14:09:35 614 [7628] DEBUG - interruption_enabled = false  
  14. 2013-01-02 14:09:35 621 [7628] DEBUG - interruption_requested = true  
  15. 2013-01-02 14:09:35 627 [7628] DEBUG - 4  
  16. 2013-01-02 14:09:35 630 [7628] DEBUG - interruption_enabled = false  
  17. 2013-01-02 14:09:35 637 [7628] DEBUG - interruption_requested = true  
  18. 2013-01-02 14:09:35 643 [7628] DEBUG - 5  
  19. 2013-01-02 14:09:35 646 [7628] DEBUG - interruption_enabled = false  
  20. 2013-01-02 14:09:35 650 [7628] DEBUG - interruption_requested = true  
  21. 2013-01-02 14:09:35 655 [7628] DEBUG - 6  
  22. 2013-01-02 14:09:35 659 [7628] DEBUG - interruption_enabled = false  
  23. 2013-01-02 14:09:35 663 [7628] DEBUG - interruption_requested = true  
  24. 2013-01-02 14:09:35 667 [7628] DEBUG - 7  
  25. 2013-01-02 14:09:35 670 [7628] DEBUG - interruption_enabled = false  
  26. 2013-01-02 14:09:35 679 [7628] DEBUG - interruption_requested = true  
  27. 2013-01-02 14:09:35 685 [7628] DEBUG - 8  
  28. 2013-01-02 14:09:35 689 [7628] DEBUG - interruption_enabled = true  

  1. 2013-01-02 14:09:35 695 [7628] DEBUG - interruption_enabled after restore = true  


网址:http://blog.csdn.net/huang_xw/article/details/8458506

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值