Document directory
- Interrupting a thread
- Catching an interruption
- Disabling interruptions
- Cooperative interruption
- Interruption is not Cancellation
One of the new features introduced in the upcoming 1.35.0 release of
Boost thread library is supportInterruptionOf a running thread. Similar to the Java and. Net interruption support, this allows for one thread to request another thread to stop at the nextInterruption point. This is the only way
Explicitly request a thread to terminate that is directly supported by the boost thread library, though users can manually implement cooperative interruption if required.
Interrupting a thread in this way is much less dangerous than brute-force tactics suchTerminateThread(), As such tactics can leave broken invariants and leak resources. If a thread is killed using a brute-force method and it was holding any
Locks, this can also potentially lead to deadlock when another thread tries to acquire those locks at some future point. interruption is also easier and more reliable than rolling your own cooperative termination scheme using mutexes, flags, condition variables,
Or some other synchronization mechanic, since it is part of the library.
Interrupting a thread
A running thread can be interrupted by callinginterrupt()Member function on the correspondingboost::threadObject. If the thread doesn' t have
boost::threadObject (e. g The initial thread of the Application), then it cannot be interrupted.
Callinginterrupt()Just sets a flag in the thread management structure for that thread and returns: It doesn' t wait for the thread to actually be interrupted. this is important, because a thread can only be interrupted at one of the predefinedInterruption
Points, And it might be that a thread never executes an interruption point, so never sees the request. Currently, the interruption points are:
boost::thread::join()boost::thread::timed_join()boost::condition_variable::wait()boost::condition_variable::timed_wait()boost::condition_variable_any::wait()boost::condition_variable_any::timed_wait()boost::this_thread::sleep()boost::this_thread::interruption_point()
When a thread reaches one of these interruption points, if interruption is enabled for that thread then it checks its interruption flag. If the flag is set, then it is cleared, andboost::thread_interruptedException is thrown. If the thread
Is already blocked on a call to one of the interruption points with interruption enabled wheninterrupt()Is called, then the thread will wake in order to throwboost::thread_interruptedException.
Catching an interruption
boost::thread_interruptedIs just a normal exception, so it can be caught, just like any other exception. this is why the "interrupted" flag is cleared when the exception is thrown-if a thread catches and handles the interruption, it is perfectly
Acceptable to interrupt it again. this can be used, for example, when a worker thread that is processing a series of independent tasks-if the current task is interrupted, the worker can handle the interruption and discard the task, and move onto the next
Task, which can then in turn be interrupted. it also allows the thread to catch the exception and terminate itself by other means, such as returning error codes, or translating the exception to pass through module boundaries.
Disabling interruptions
Sometimes it is necessary to avoid being interrupted for a participant section of code, such as in a destructor where an exception has the potential to cause immediate Process Termination. This is done by constructing an instanceboost::this_thread::disable_interruption.
Objects of this class disable interruption for the thread that created them on construction, and restore the interruption state to whatever it was before on destruction:
void f(){// interruption enabled here{boost::this_thread::disable_interruption di;// interruption disabled{boost::this_thread::disable_interruption di2;// interruption still disabled} // di2 destroyed, interruption state restored// interruption still disabled} // di destroyed, interruption state restored// interruption now enabled}
Effects of an instanceboost::this_thread::disable_interruptionCan be temporarily reversed by constructing an instanceboost::this_thread::restore_interruption, Passing in
boost::this_thread::disable_interruptionObject in question. This will restore the interruption state to what it was whenboost::this_thread::disable_interruptionObject was constructed, and then disable interruption again when
Theboost::this_thread::restore_interruptionObject is destroyed:
void g(){// interruption enabled here{boost::this_thread::disable_interruption di;// interruption disabled{boost::this_thread::restore_interruption ri(di);// interruption now enabled} // ri destroyed, interruption disabled again{boost::this_thread::disable_interruption di2;// interruption disabled{boost::this_thread::restore_interruption ri2(di2);// interruption still disabled// as it was disabled when di2 constructed} // ri2 destroyed, interruption still disabled} //di2 destroyed, interruption still disabled} // di destroyed, interruption state restored// interruption now enabled}
boost::this_thread::disable_interruptionAndboost::this_thread::restore_interruptionCannot be moved or copied, and they are the only way of enabling and disabling interruption. This ensures that the interruption state is correctly
Restored when the scope is exited (whether normally, or by an exception ), and that you cannot enable interruptions in the middle of an interruption-disabled block unless you're in full control of the Code, and have access toboost::this_thread::disable_interruption
Instance.
At any point, the interruption state for the current thread can be queried by callingboost::this_thread::interruption_enabled().
Cooperative interruption
As well as the Interruption Points on blocking operations suchsleep()Andjoin(), There is one interruption point explicitly designed to allow interruption at a user-designated point in the code.boost::this_thread::interruption_point()
Does nothing should t check for an interruption, and can therefore be used in long-running code that doesn't execute any other interruption points, in order to allow for cooperative interruption. just like the other interruption points,
interruption_point()Respects the interruption enabled state, and does nothing if interruption is disabled for the current thread.
Interruption is not Cancellation
On POSIX platforms, threads can be canceled rather than killed, by calling
pthread_cancel(). This is similar to interruption, but is a separate mechanic, with different behaviour. in particle, cancellation cannot be stopped once it is started: whereas interruption just throws an exception, once a cancellation request has
Been acknowledged the thread is already tively dead.pthread_cancel()Does not always execute Destructors either (though it does on some platforms), as it is primarily a C interface-if you want to clean up your resources when a thread is canceled,
You need to usepthread_cleanup_push()To register a cleanup handler. The advantage here is thatpthread_cleanup_push()Works in C stack frames, whereas exceptions don't play nicely in C: On some platforms it will crash your program
For an exception to propagate into a C stack frame.
For portable code, I recommend interruption over cancellation. it's supported on all platforms that can use the boost thread library, and it works well with C ++ code-it's just another exception, so all your Destructors and catch blocks work just fine.
Posted by Anthony Williams
From: http://www.justsoftwaresolutions.co.uk/threading/thread-interruption-in-boost-thread-library.html