at_thread_exit() never getting called

View: New views
20 Messages — Rating Filter:   Alert me  
< Prev | 1 - 2 | Next >

at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

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? Is there a way to hook the main thread shutting down?

Thanks,
Gili

Re: at_thread_exit() never getting called

by Steven Watanabe-4 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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...

> Is there a way to hook the main thread
> shutting down?
>  

On windows you can use on_thread_exit().

In Christ,
Steven Watanabe

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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...

> Is there a way to hook the main thread
> shutting down?
>  

On windows you can use on_thread_exit().

In Christ,
Steven Watanabe

_______________________________________________
Boost-users mailing list
Boost-users@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by Vicente Botet Escriba :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
----- Original Message -----
From: "cowwoc" <cowwoc@...>
To: <boost-users@...>
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


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by Bugzilla from anthony.ajw@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"vicente.botet" <vicente.botet@...> 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

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by Vicente Botet Escriba :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,
----- Original Message -----
From: "Anthony Williams" <anthony.ajw@...>
To: <boost-users@...>
Cc: <boost@...>
Sent: Thursday, October 29, 2009 9:49 AM
Subject: Re: [Boost-users] at_thread_exit() never getting called


>
> "vicente.botet" <vicente.botet@...> 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


_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bugzilla from anthony.ajw@gmail.com wrote:
"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
Anthony,

The documentation is far from obvious on this point. It just reads: "This copy is invoked when the current thread exits (even if the thread has been interrupted)."

Source: http://www.boost.org/doc/libs/1_40_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread.atthreadexit

I expect the function to get invoked if the thread exits for *any* reason, not just because the documentation says so but also because it's what I want from a coding point of view.

Thanks,
Gili

Re: at_thread_exit() never getting called

by Bugzilla from anthony.ajw@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

"vicente.botet" <vicente.botet@...> writes:

> From: "Anthony Williams" <anthony.ajw@...>
>> 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 mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by Bugzilla from anthony.ajw@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cowwoc <cowwoc@...> writes:

> The documentation is far from obvious on this point. It just reads: "This
> copy is invoked when the current thread exits (even if the thread has been
> interrupted)."
>
> Source:
> http://www.boost.org/doc/libs/1_40_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread.atthreadexit
>
> I expect the function to get invoked if the thread exits for *any* reason,
> not just because the documentation says so but also because it's what I want
> from a coding point of view.

OK, I'll update the documentation.

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 mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Bugzilla from anthony.ajw@gmail.com wrote:
cowwoc <cowwoc@bbs.darktech.org> writes:

> The documentation is far from obvious on this point. It just reads: "This
> copy is invoked when the current thread exits (even if the thread has been
> interrupted)."
>
> Source:
> http://www.boost.org/doc/libs/1_40_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread.atthreadexit
>
> I expect the function to get invoked if the thread exits for *any* reason,
> not just because the documentation says so but also because it's what I want
> from a coding point of view.

OK, I'll update the documentation.

Anthony
I'd rather you fix the undesirable behavior instead...

Gili

Re: at_thread_exit() never getting called

by Eric J. Holtman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cowwoc wrote:
> I'd rather you fix the undesirable behavior instead...
>
>  

You could submit a patch file.
_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by Bugzilla from anthony.ajw@gmail.com :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cowwoc <cowwoc@...> writes:

> Bugzilla from anthony.ajw@... wrote:
>>
>> cowwoc <cowwoc@...> writes:
>>
>>> The documentation is far from obvious on this point. It just reads: "This
>>> copy is invoked when the current thread exits (even if the thread has
>>> been
>>> interrupted)."
>>>
>>> Source:
>>> http://www.boost.org/doc/libs/1_40_0/doc/html/thread/thread_management.html#thread.thread_management.this_thread.atthreadexit
>>>
>>> I expect the function to get invoked if the thread exits for *any*
>>> reason,
>>> not just because the documentation says so but also because it's what I
>>> want
>>> from a coding point of view.
>>
>> OK, I'll update the documentation.
>
> I'd rather you fix the undesirable behavior instead...

OK, what is your desired behaviour in these circumstances?

(1) main() thread uses at_thread_exit() and then main() returns

(2) main() thread uses at_thread_exit() and calls exit()

(3) main() thread uses at_thread_exit(). *Another* thread calls exit()

(4) *another* thread uses at_thread_exit(). main() returns

(5) *another* thread uses at_thread_exit(). main() calls exit()

(6) *another* thread uses at_thread_exit() and then calls exit()

Note that returning from main() is the same as calling exit(), and
abruptly terminates all 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

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Anthony Williams-4 wrote:
OK, what is your desired behaviour in these circumstances?

(1) main() thread uses at_thread_exit() and then main() returns

(2) main() thread uses at_thread_exit() and calls exit()

(3) main() thread uses at_thread_exit(). *Another* thread calls exit()

(4) *another* thread uses at_thread_exit(). main() returns

(5) *another* thread uses at_thread_exit(). main() calls exit()

(6) *another* thread uses at_thread_exit() and then calls exit()

Note that returning from main() is the same as calling exit(), and
abruptly terminates all threads.
Hi Anthony,

For all cases mentioned above I would expect the shutdown hook to get invoked. If a thread (main or otherwise) exits for any reason (due to "return" or exit()) I would want the shutdown hook to get invoked so I can do the proper cleanup. Do you foresee any problem with this behavior?

Thanks,
Gili

Re: at_thread_exit() never getting called

by Peter Dimov-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cowwoc wrote:

> Anthony Williams-4 wrote:
>>
>> OK, what is your desired behaviour in these circumstances?
>>
>> (1) main() thread uses at_thread_exit() and then main() returns
>>
>> (2) main() thread uses at_thread_exit() and calls exit()
>>
>> (3) main() thread uses at_thread_exit(). *Another* thread calls
>> exit()
>>
>> (4) *another* thread uses at_thread_exit(). main() returns
>>
>> (5) *another* thread uses at_thread_exit(). main() calls exit()
>>
>> (6) *another* thread uses at_thread_exit() and then calls exit()
>>
>> Note that returning from main() is the same as calling exit(), and
>> abruptly terminates all threads.
>
> Hi Anthony,
>
> For all cases mentioned above I would expect the shutdown hook to get
> invoked.

The at_thread_exit hooks are invoked in the context of the thread. When exit
is called, the threads are executing something, and it is not possible to
make them stop whatever they are doing in order to execute the
at_thread_exit hooks (and it would be a bad idea were it possible, because
the thread state could be inconsistent and cause a crash in the hook.)

It's possible to execute the at_thread_exit hooks just for the thread that
called exit(), but this is inconsistent and effectively duplicates atexit.

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Dimov-5 wrote:
The at_thread_exit hooks are invoked in the context of the thread. When exit
is called, the threads are executing something, and it is not possible to
make them stop whatever they are doing in order to execute the
at_thread_exit hooks (and it would be a bad idea were it possible, because
the thread state could be inconsistent and cause a crash in the hook.)
Peter,

Are you saying that if I register shutdown hooks for threads 1, 2 and 3 and invoke exit() from thread 3 the current behavior of at_thread_exit() would cause the shutdown hooks to all get invoked by thread 3 while thread 1 and 2 are still executing?

Thanks,
Gili

Re: at_thread_exit() never getting called

by Peter Dimov-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cowwoc wrote:

> Peter Dimov-5 wrote:
>>
>> The at_thread_exit hooks are invoked in the context of the thread.
>> When exit
>> is called, the threads are executing something, and it is not
>> possible to make them stop whatever they are doing in order to
>> execute the at_thread_exit hooks (and it would be a bad idea were it
>> possible, because the thread state could be inconsistent and cause a
>> crash in the hook.)
>>
>
> Peter,
>
> Are you saying that if I register shutdown hooks for threads 1, 2 and
> 3 and invoke exit() from thread 3 the current behavior of
> at_thread_exit() would cause the shutdown hooks to all get invoked by
> thread 3 while thread 1 and 2 are still executing?

I'm not sure what the current behavior is, but in this scenario, no hooks
should be executed. I think that this is consistent with how TSD destructors
work under POSIX, but I can't test this at the moment.

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Peter Dimov-5 wrote:
I'm not sure what the current behavior is, but in this scenario, no hooks
should be executed. I think that this is consistent with how TSD destructors
work under POSIX, but I can't test this at the moment.
Unless I missed something, I don't think that the scenario you propose would work for my use-case. Basically, I've got a thread that runs in an endless loop until the program shuts down. When the program shuts down, I want it to clean up some resources cleanly. The way I've done this is by registering a at_thread_exit() hook that causes the aforementioned thread to shut down cleanly (breaking out of the endless loop) thereby allowing it to clean up its resources cleanly. I don't want to user of my API to have to explicitly invoke dispose() on this thread because it is initialized lazily and is just a side-effect of my particular implementation (other implementations can do without it).

How do you propose I implement this with the scenario you outlined?

Thank you,
Gili

Re: at_thread_exit() never getting called

by Peter Dimov-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

cowwoc wrote:
> Unless I missed something, I don't think that the scenario you
> propose would work for my use-case. Basically, I've got a thread that
> runs in an endless loop until the program shuts down. When the
> program shuts down, I want it to clean up some resources cleanly. The
> way I've done this is by registering a at_thread_exit() hook that
> causes the aforementioned thread to shut down cleanly (breaking out
> of the endless loop) thereby allowing it to clean up its resources
> cleanly.

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.

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users

Re: at_thread_exit() never getting called

by cowwoc :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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...

Gili

Re: at_thread_exit() never getting called

by Peter Dimov-5 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

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.

_______________________________________________
Boost-users mailing list
Boost-users@...
http://lists.boost.org/mailman/listinfo.cgi/boost-users
< Prev | 1 - 2 | Next >