当线程在特定的作用域中时,我试图防止线程中断。但是,使用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();
}文档似乎暗示,当di在作用域中时,boost::this_thread::sleep()不会抛出boost::thread_interrupted。
我做错了什么?
发布于 2011-08-08 06:33:08
您应该删除以下行中的括号:
//boost::this_thread::disable_interruption di();
boost::this_thread::disable_interruption di;您没有创建disable_interruption对象,而是声明了function di。
https://stackoverflow.com/questions/6976161
复制相似问题