[Ace-users] [ace-users] detach ACE_Thread

Andriy Gapon avg at icyb.net.ua
Tue Dec 18 10:05:48 CST 2007

on 17/12/2007 15:07 Douglas C. Schmidt said the following:
>> Some questions:
>> 1). I can not seem to find anything similar to pthread_detach() in
>> ACE_Thread/ACE_Thread_Manager interface. Does ACE provide such
>> capability at all ?
> No, it doesn't (yet).
>> 2). Assuming an answer to the previous question would be "no", is there
>> a way I could call pthread_* functions directly to "hack" to get what I
>> want and not confuse ACE thread management ?
> It seems to be that a better way to handle this would be to enhance
> ACE_Thread_Manager so it supports a detact() method that does what you
> describe above, e.g., call pthread_detach() on platforms that support
> it and also update the internals of the ACE_Thread_Manager so that
> it'll update its internals.  


yes, it would be great to write this, but I am not sure if I could
understand all the internal code, especially the variations for
different platforms.
Unfortunately I am short on time now, so maybe some time later.

Just a small note for now: it is a very bad idea to simply call
pthread_detach on a THR_JOINABLE thread created via ACE_Thread_Manager.
First, some resources within the manager will not be cleaned up anyway
(some data will be preserved there for a possible future join). Second,
if the thread id is reused for another thread the manager can become
confused and a deadlock can happen at the very least. This can be easily
observed at least on 2.6.19 linux kernel where a thread id is re-used
very quickly after thread termination (underlying implementation can do
it because the thread became detached). (This happens because it is
assumed that pthread_join on a terminated thread is non-blocking and
quick, so it is done without releasing a mutex, but it turns out that
the thread is not terminated it just re-used the id).

Using ACE_Thread static methods for thread management is friendlier to
pthread_detach, but there are other shortcomings with that.

P.S. the described above issue with deadlock makes me wonder if
ACE_Thread_Manager is immune to thread id re-use by underlying pthread
library if joinable threads are created and terminated but join is
called some time later.

Andriy Gapon

More information about the Ace-users mailing list