【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?
- namespace
- {
- boost::mutex io_mu;
- void to_interrupt(const std::string& str)
- {
- // 如果在线程外部调用了this_thread->interrupt()
- // 线程内部的以下这些检查点可以抛出boost::thread_interrupted异常
- try
- {
- boost::this_thread::disable_interruption();
- for (int i = 0; i < 5; ++i)
- {
- boost::mutex::scoped_lock lock(io_mu);
- PRINT_DEBUG(i);
- PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
- // PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_requested());
- if (i == 2)
- {
- PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
- boost::this_thread::interruption_point();
- PRINT_DEBUG(std::boolalpha << boost::this_thread::interruption_enabled());
- }
- }
- }
- catch (boost::thread_interrupted &)
- {
- }
- }
- }
- void test_thread_interrupt()
- {
- boost::thread t(to_interrupt, "hello");
- // 中断函数
- t.interrupt();
- t.join();
- }
[cpp] view plaincopy print?
- 2013-01-02 11:00:44 263 [8272] DEBUG - 0
- 2013-01-02 11:00:44 266 [8272] DEBUG - 1
- 2013-01-02 11:00:44 269 [8272] DEBUG - 2
- 2013-01-02 11:02:06 555 [5168] DEBUG - 0
- 2013-01-02 11:02:06 559 [5168] DEBUG - 1
- 2013-01-02 11:02:06 561 [5168] DEBUG - 2
- 2013-01-02 11:02:06 564 [5168] DEBUG - 3
- 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?
- namespace
- {
- boost::mutex io_mu;
- void to_interrupt_disable(const std::string& str)
- {
- // 默认可以中断
- assert(boost::this_thread::interruption_enabled());
- for (int i = 0; i < 10; i++)
- {
- // 关闭中断
- boost::this_thread::disable_interruption di;
- // 此时中断不可用
- PRINT_DEBUG(std::boolalpha << "interruption_enabled = " << boost::this_thread::interruption_enabled());
- // 是否有中断请求
- PRINT_DEBUG(std::boolalpha << "interruption_requested = " << boost::this_thread::interruption_requested());
- boost::mutex::scoped_lock lock(io_mu);
- PRINT_DEBUG(i);
- // 使用中断点函数,因为关闭中断,此时无效果。 中断恢复后,它才生效。
- boost::this_thread::interruption_point();
- if (i == 8)
- {
- // 临时恢复中断
- boost::this_thread::restore_interruption ri(di);
- PRINT_DEBUG(std::boolalpha << "interruption_enabled = " << boost::this_thread::interruption_enabled());
- PRINT_DEBUG(std::boolalpha << "interruption_enabled after restore = " << boost::this_thread::interruption_enabled());
- boost::this_thread::interruption_point();
- }
- }
- }
- }
- void test_thread_interrupt_disable()
- {
- boost::thread t(to_interrupt_disable, "hello");
- t.interrupt();
- t.join();
- }
[cpp] view plaincopy print?
- 2013-01-02 14:09:35 538 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 544 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 551 [7628] DEBUG - 0
- 2013-01-02 14:09:35 555 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 563 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 570 [7628] DEBUG - 1
- 2013-01-02 14:09:35 574 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 581 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 586 [7628] DEBUG - 2
- 2013-01-02 14:09:35 589 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 601 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 608 [7628] DEBUG - 3
- 2013-01-02 14:09:35 614 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 621 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 627 [7628] DEBUG - 4
- 2013-01-02 14:09:35 630 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 637 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 643 [7628] DEBUG - 5
- 2013-01-02 14:09:35 646 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 650 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 655 [7628] DEBUG - 6
- 2013-01-02 14:09:35 659 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 663 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 667 [7628] DEBUG - 7
- 2013-01-02 14:09:35 670 [7628] DEBUG - interruption_enabled = false
- 2013-01-02 14:09:35 679 [7628] DEBUG - interruption_requested = true
- 2013-01-02 14:09:35 685 [7628] DEBUG - 8
- 2013-01-02 14:09:35 689 [7628] DEBUG - interruption_enabled = true
- 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不会中断当前线程。本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
