I am trying to prevent interruption of a thread while it is in a particular scope. However, using boost::this_thread::disable_interruption di()
does not seem to have any effect.
#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();
}
The documentation appears to imply that boost::this_thread::sleep()
will not throw boost::thread_interrupted
while di
is in scope.
What am I doing wrong?