我试图防止线程在特定范围内中断。但是,使用 boost::this_thread::disable_interruption di()
似乎没有任何效果。
#include <boost/thread.hpp>
#include <iostream>
void worker() {
std::cout << "START" << std::endl;
for(;;) {
{
boost::this_thread::disable_interruption di();
try {
boost::this_thread::sleep(boost::posix_time::seconds(1));
}
catch(boost::thread_interrupted & e) {
assert( false );
}
}
try {
boost::this_thread::interruption_point();
}
catch(boost::thread_interrupted & e) {
break;
}
}
std::cout << "END" << std::endl;
}
int main() {
boost::thread thread(&worker);
thread.interrupt();
thread.join();
}
文档似乎暗示 boost::this_thread::sleep()
在 di
处于运行状态时不会抛出 boost::thread_interrupted
范围。
我做错了什么?
最佳答案
您应该删除以下行中的括号:
//boost::this_thread::disable_interruption di();
boost::this_thread::disable_interruption di;
您没有创建disable_interruption对象,而是声明了函数di。
关于c++ - boost disable_interruption 不阻止 thread_interrupted,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6976161/