
Dhanvi Kapila wrote:
by exit handlers I mean some special function that would be gauranteed to be called whenever the thread is exiting.
Technically, this depends entirely on whether the underlying operating
system provides such a mechanism. Win32 doesn't. A thread killed by
TerminateThread, or simply ended by ExitThread, is dead immediately, and
no code is ever again executed on its stack. The only thing that still
gets executed (not necessarily on the existing thread) are the DLL entry
functions.
Thus, Boost.Thread cannot possibly supply a reliable version of such a
feature across all platforms, and as such, it's probably best that it
doesn't supply the feature at all.
If you want to guarantee execution of a function on thread end under
normal circumstances (i.e. normal return or exception stack unwinding),
you should use a sentry object that executes that code in the destructor:
void thread_entry()
{
thread_sentry sentry;
// Code here
}
You can easily make a reusable object for this:
class thread_sentry : public boost::noncopyable
{
boost::function