Boost logo

Boost Users :

Subject: Re: [Boost-users] at_thread_exit() never getting called
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2009-11-09 03:27:00


cowwoc <cowwoc_at_[hidden]> writes:

> Peter Dimov-5 wrote:
>>
>> exit() will not hang, it will just terminate the thread (and all other
>> threads) as its very last step (after invoking the atexit handlers and
>> global/static destructors).
>>
>
> But what happens if one of the handlers is itself waiting for the thread to
> shut down?

If an atexit handler waits for a thread to shut down then it will
block. If it is waiting for its own thread to shutdown then you have
deadlock. That's easy to fix --- just set a flag to indicate to the
atexit handler not to wait for this thread before you call exit.

> Also, is at_thread_exit() implemented as an atexit() handler under the hood?

at_thread_exit is completely unrelated to atexit. It runs a function
when the thread that called at_thread_exit returns from its thread
function. For threads started with boost::thread this is done as part of
the boost::thread internal thread management code. For threads started
with platform-specific facilities such as CreateThread or pthread_create
it is done using platform-specific hooks. Again, these do not interact
with atexit handlers.

> If not, how do you prevent it from killing the hook functions prematurely?

Calling exit() terminates all threads abruptly once the atexit handlers
have run. Thread exit functions are not run for any threads. If threads
are running at_thread_exit hooks they are terminated just as abruptly.

Anthony

-- 
Author of C++ Concurrency in Action | http://www.manning.com/williams
just::thread C++0x thread library   | http://www.stdthread.co.uk
Just Software Solutions Ltd         | http://www.justsoftwaresolutions.co.uk
15 Carrallack Mews, St Just, Cornwall, TR19 7UL, UK. Company No. 5478976

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