Boost logo

Boost Users :

Subject: Re: [Boost-users] at_thread_exit() never getting called
From: Anthony Williams (anthony.ajw_at_[hidden])
Date: 2009-10-29 09:54:19


"vicente.botet" <vicente.botet_at_[hidden]> writes:

> From: "Anthony Williams" <anthony.ajw_at_[hidden]>
>> If you call pthread_exit(0) from main() then thread exit handlers *will*
>> be called. Of course, this doesn't exit the program until all other
>> threads have terminated, whereas "return 0" kills the other threads.
>
> Unfortunately, after rereading the patch I proposed, I recognize the
> patch don't works if there are other external threads in the programm
> as in patch stores only the last external thread context.

Yes. That's one reason I didn't apply it.

> Calling pthread_exit(0) on a pthread thread is natural, but the main
> thread is no created as a pthread, so this call is not portable.

True; it's only portable among POSIX platforms, which sort-of defeats
the point of using boost.

> Do
> you think that we need a portable function boost::exit that will call
> to pthread_exit?

This is tricky. pthread_exit() and it's Windows counterpart ExitThread
abrubtly end the thread when called, without unwinding the stack. In
C++, we probably want the stack to unwind, in order for all our nice
RAII objects to release their resources.

> With the current implementation we don't have any error when we call
> at_thread_exit on an external thread. This has as consequence that the
> destructors of the TSS are not called. This is a severe restriction
> from my point of view.

at_thread_exit works on every thread: the cleanup runs when the thread
exits "normally". If you call exit() then cleanup functions are not
run. The problem is that returning from main() is equivalent to calling
exit() (and thus skips the cleanup), whereas returning from any other
thread function just exits that thread (and does the cleanup).

> Do you have a portable solution?

Don't do any work in main(): spawn a new thread and have main() wait for
it.

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