Re: [boost] [Boost-users] at_thread_exit() never getting called

Hi, ----- Original Message ----- From: "cowwoc" <cowwoc@bbs.darktech.org> To: <boost-users@lists.boost.org> Sent: Wednesday, October 28, 2009 7:33 PM Subject: Re: [Boost-users] at_thread_exit() never getting called
I think https://svn.boost.org/trac/boost/ticket/2739 prevents it from working.
Gili
Steven Watanabe-4 wrote:
AMDG
cowwoc wrote:
I am registering boost::this_thread::at_thread_exit() against one of my threads. When the main thread exits using "return 0" the at_thread_exit hook never gets invoked. Is this normal?
It's supposed to work for the main thread...
For me yes, but it dosn't works. The example attached to the ticket show the problem.
Is there a way to hook the main thread shutting down?
On windows you can use on_thread_exit().
The patch worked when I summited it. Anthony, I've see that you have started to integrate the patch for the thread attributes. Do you plan to integrate this patch also? Thanks, Vicente

"vicente.botet" <vicente.botet@wanadoo.fr> writes:
cowwoc wrote:
I am registering boost::this_thread::at_thread_exit() against one of my threads. When the main thread exits using "return 0" the at_thread_exit hook never gets invoked. Is this normal?
If you call exit() then thread exit handlers are not called. "return 0" in main() is the same as calling exit(0), so thread exit handlers are not called.
Anthony, I've see that you have started to integrate the patch for the thread attributes. Do you plan to integrate this patch also?
No. 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. 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

Hi, ----- Original Message ----- From: "Anthony Williams" <anthony.ajw@gmail.com> To: <boost-users@lists.boost.org> Cc: <boost@lists.boost.org> Sent: Thursday, October 29, 2009 9:49 AM Subject: Re: [Boost-users] at_thread_exit() never getting called
"vicente.botet" <vicente.botet@wanadoo.fr> writes:
cowwoc wrote:
I am registering boost::this_thread::at_thread_exit() against one of my threads. When the main thread exits using "return 0" the at_thread_exit hook never gets invoked. Is this normal?
If you call exit() then thread exit handlers are not called. "return 0" in main() is the same as calling exit(0), so thread exit handlers are not called.
Anthony, I've see that you have started to integrate the patch for the thread attributes. Do you plan to integrate this patch also?
No.
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. 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. Do you think that we need a portable function boost::exit that will call to pthread_exit? 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. Do you have a portable solution? Best, Vicente

"vicente.botet" <vicente.botet@wanadoo.fr> writes:
From: "Anthony Williams" <anthony.ajw@gmail.com>
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

----- Original Message ----- From: "Anthony Williams" <anthony.ajw@gmail.com> To: <boost@lists.boost.org> Cc: <boost-users@lists.boost.org> Sent: Thursday, October 29, 2009 2:54 PM Subject: Re: [boost] at_thread_exit() never getting called
"vicente.botet" <vicente.botet@wanadoo.fr> writes:
From: "Anthony Williams" <anthony.ajw@gmail.com>
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.
OK, I see. Please could you add this resolution to the ticket and add a warning and this guideline to the Boost.Thread documentation? Thanks, Vicente
participants (2)
-
Anthony Williams
-
vicente.botet