当您想要测试interruption_rerequested()时,为什么要使用boost disable_interr

Why use boost disable_interruption when you want to test for interruption_requested()?

本文关键字:为什么 boost interr disable 测试 interruption rerequested      更新时间:2023-10-16

我在很多地方看到这样的代码:

void threadFunction()
{
    boost::this_thread::disable_interruption disable;
    while (!boost::this_thread::interruption_requested())
    {
            //do stuff
    }
}

对我来说,这看起来像是我"禁用"了线程函数,使其不被中断,但随后我再次测试中断。神奇的是,它起作用了。有人能向我解释一下它在幕后的实际作用吗?非常感谢。

disable_interruption防止线程实际被中断;它不会阻止设置中断状态。然后interruption_requested测试中断状态是否已设置

参见Boost文档:具体地;中断";区段

各种方法被归类为"中断点",如果在请求中断时调用它们,则会引发异常,除非中断已被禁用。

简而言之:

  • 禁用中断可防止中断实际停止线程,但中断仍将线程标记为"请求中断"
  • interruption_requested方法检查是否已请求中断
相关文章: