【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()则是一个特殊的中断点函数,它并不等待,只是起到一个标签的作用,表示线程执行到这个函数所在的语句就可以被中断。看看下面的例子:

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
下面谈谈启用/禁用线程中断
缺省情况下钱程都是允许中断的,但thread库允许控制线程的中断行为。
thread 库在子名字空间this_thread提供了一组函数和类来共同完成线程的中断启用和禁用:
1. interruption_enabled(): 函数检测当前线程是否允许中断
2. interruption_requested(): 函数检测当前线程是否被要求中断
3. 类disable_interruption是一个RAII类型的对象,它在构造时关闭线程的中断,析构时自动恢复线程的中断状态。在disable_interruption 的生命期内线程始终是不可中断的,除非使用了restore_interruption 对象。
4. restore_interruption只能在disable_interruption 的作用域内使用,它在构造时临时打开线程的中断状态,在析构时又关闭中断状态。
这些中断点中的前8个都是某种形式的等待函数,表明线程在阻塞等待的时候可以被中断。

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

你可能感兴趣的:(【Boost】boost库中thread多线程详解5——谈谈线程中断)