Search code examples
c++boost-thread

How to interrupt boost::this_thread in c++


In my application i have given sleep of 10 sec. I have given sleep using boost::this_thread::sleep function. Is there any possible way to interrupt boost::this_thread::sleep function.?


Solution

  • From the sleep() reference:

    Throws: boost::thread_interrupted if the current thread of execution is interrupted.

    Notes: sleep() is one of the predefined interruption points.

    So what you need to do in your thread is to put the sleep() call in a try-catch, and catch boost::thread_interrupted. Then call interrupt() on the thread object to interrupt the sleep.


    By the way, also from the sleep reference:

    Warning

    DEPRECATED since 3.0.0.

    Use sleep_for() and sleep_until() instead.