【Boost】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 plaincopy 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 plaincopy 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 plaincopy 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 plaincopy 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 plaincopy 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  
  29. 2013-01-02 14:09:35 695 [7628] DEBUG - interruption_enabled after restore = true  



Interruption机制:
可以通过thread对象的interrupt函数,通知线程,需要interrupt。线程运行到interruption point就可以退出。
Interruption机制举例:
#include "stdafx.h"
#include 
#include 
using namespace std;void f()
{for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<>24)==5){boost::this_thread::interruption_point();}}}
}int _tmain(int argc, _TCHAR* argv[])
{boost::thread t(f);t.interrupt();t.join();  //等待线程结束return 0;
}
t.interrupt();告诉t线程,现在需要interrupt。boost::this_thread::interruption_requested()可以得到当前线程是否有一个interrupt请求。若有interrupt请求,线程在运行至interruption点时会结束。boost::this_thread::interruption_point();就是一个interruption point。Interruption point有多种形式,较常用的有boost::this_thread::sleep(boost::posix_time::seconds(5));当没有interrupt请求时,这条语句会让当前线程sleep五秒,若有interrupt requirement线程结束。
如何使线程在运行到interruption point的时候,不会结束,可以参考下面的例子:

#include "stdafx.h"
#include 
#include 
using namespace std;void f()
{for(int i=1;i<0x0fffffff;i++){if(i%0xffffff==0){cout<<"i="<<((i&0x0f000000)>>24)<>24)==5){boost::this_thread::disable_interruption di;{boost::this_thread::interruption_point();}}}}
}int _tmain(int argc, _TCHAR* argv[])
{boost::thread t(f);t.interrupt();t.join();  //等待线程结束return 0;
}
注意boost::this_thread::disable_interruption这条语句的使用,它可以使大括号内的interruption point不会中断当前线程。


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部