Boost Users :
From: Sebastian Redl (sebastian.redl_at_[hidden])
Date: 2006-02-10 13:09:37
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
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:
// Code here
You can easily make a reusable object for this:
class thread_sentry : public boost::noncopyable
boost::function<void ()> exit_func_;
thread_sentry(boost::function<void ()> exit_func)
Now you can do:
// Cleanup code.
// Code here
Usually such code can be avoided by using RAII objects for all resources
in the first place.
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net