C++ boost disable_中断不阻止线程_中断
我试图防止线程在特定范围内中断。但是,使用C++ boost disable_中断不阻止线程_中断,c++,boost,boost-thread,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::thi
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();
}
#包括
#包括
void worker(){
std::cout您应该删除下一行中的括号:
//boost::this_thread::disable_interruption di();
boost::this_thread::disable_interruption di;
您没有创建禁用中断对象,而是声明了函数di。谢谢,这解决了问题!不过,您能解释一下它是如何声明函数的吗?disable\u interruption
是一种类型。@Timesquare:这是一个有趣的问题。您可以阅读它。