
cowwoc wrote:
Peter Dimov-5 wrote:
You can't use an at_thread_exit hook registered from within a thread to shut down this same thread; this is circular. If you want to shut down a thread when exit() is called or when main returns, use atexit, or a destructor of a static variable. I don't see how at_thread_exit could help in this case, although of course I may be misunderstanding something.
Part of the problem is that neither atexit() or at_thread_exit() explicitly specify which thread the hook function will get invoked on...
atexit handlers (and global/static variable destructors) are invoked by the thread that calls exit() (or returns from main). at_thread_exit handlers (and thread-specific variable destructors) should be invoked by the thread that called at_thread_exit, after it exits from its thread function.