Tim*_*are 2 c++ boost boost-thread
我试图防止线程在特定范围内时中断.但是,使用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();
}
Run Code Online (Sandbox Code Playgroud)
文档似乎暗示boost::this_thread::sleep()不会抛出boost::thread_interrupted,而di在范围内.
我究竟做错了什么?
您应该删除以下行中的括号:
//boost::this_thread::disable_interruption di();
boost::this_thread::disable_interruption di;
Run Code Online (Sandbox Code Playgroud)
您没有创建disable_interruption对象,而是声明了函数di.