Posix thread join problem

Posix thread join problem

Post by ZoombyWoo » Thu, 31 May 2001 05:53:47



Hi. In Solaris you can use Solaris's thread routines like thr_join. thr_join
is a very useful routine in that it can wait for any of you applications
created thread, and when one of you threads exit, thr_join lets go and it
tells you which thread that exited. This is a very easy way to catch thread
exits and to be able to take appropriate action depending of which thread
that exited.
I want to do the same thing in Linux, but I haven't been able to do so. The
Posix version of joining a thread is pthread_join, which can only wait for 1
specific thread, you give the thread id as an argument to pthread_join.
The Solaris version is much more useful (at least for me:-)

Anyone has an idea of how I can either get a work-around, or if there is
some other library I can use ?

I run RedHat 7.1 ,using gcc 2.96

Thanx

/ZoombyWoof

 
 
 

Posix thread join problem

Post by David Tanze » Thu, 31 May 2001 15:13:58


Hi.


> Hi. In Solaris you can use Solaris's thread routines like thr_join. thr_join
> is a very useful routine in that it can wait for any of you applications
> created thread, and when one of you threads exit, thr_join lets go and it
> tells you which thread that exited. This is a very easy way to catch thread
> exits and to be able to take appropriate action depending of which thread
> that exited.
> I want to do the same thing in Linux, but I haven't been able to do so. The
> Posix version of joining a thread is pthread_join, which can only wait for 1
> specific thread, you give the thread id as an argument to pthread_join.
> The Solaris version is much more useful (at least for me:-)

> Anyone has an idea of how I can either get a work-around, or if there is
> some other library I can use ?

Found this in the pthread - man:

- void pthread_cleanup_push(void (*cleanup)(void *), void *arg);
-
- A thread may register cleanup handlers which are automatically called
on
- behalf of the thread when it terminates either through cancellation
[see
- pthread_cancel()], explicitly exiting [see pthread_exit()] or by
- returning from its start function [see pthread_create()].  Handlers
are
- run in order: most recently registered first.

Maybe that's a workaround and there are better ways to do this, but you
could maybe use these cleanup handlers to catch thread exits:
You install one default cleanup handler in every thread, which would
then
set a static variable, which identifies the thread which has exited.
Then you could write a listener function, which tells you when a thread
has exited.

Quote:> [...]


 
 
 

Posix thread join problem

Post by Kaz Kylhe » Thu, 31 May 2001 23:59:33



>I want to do the same thing in Linux, but I haven't been able to do so. The
>Posix version of joining a thread is pthread_join, which can only wait for 1
>specific thread, you give the thread id as an argument to pthread_join.
>The Solaris version is much more useful (at least for me:-)

Sure, but the problem is you have to install an entire proprietary operating
system to use the nonstandard function! If you know what you are doing, you
should be able to write something that provides the same functionality. Here
is a candidate design: have your terminating threads add a message to a shared
queue and hit a condition variable.  Some thread can wait on the condition and
then examine the queue to see what threads have terminated (or are about to)
and do the corresponding pthread_joins.