2

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?

Timesquare
  • 399
  • 2
  • 12

1 Answers1

5

You should remove parenthesis in the following line:

//boost::this_thread::disable_interruption di();
boost::this_thread::disable_interruption di;

Instead of creating disable_interruption object, you declared function di.

eugene_che
  • 1,997
  • 12
  • 12
  • Thanks, this solved the problem! Though, could you explain how it was declaring a function? `disable_interruption` is a type. – Timesquare Aug 07 '11 at 22:52
  • @Timesquare: it is an interesting issue. You can read about it [here](http://stackoverflow.com/questions/1034606/is-there-any-use-for-local-function-declarations). – eugene_che Aug 07 '11 at 22:56