thread库预定义了共9个中断点,它们都是函数,如下:
1. thread::join();而最后一个位于子名字空间this_thread的interruption_point()则是一个特殊的中断点函数,它并不等待,只是起到一个标签的作用,表示线程执行到这个函数所在的语句就可以被中断。看看下面的例子:
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(); }运行结果:
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如果注释boost::this_thread::interrupt_point了,则结果如下:
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下面谈谈启用/禁用线程中断
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(); }结果:
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