Waiting for thread termination
One thread can suspend itself to wait for the termination of another thread
with the
thr_join(3thread)
function
int thr_join(
thread_t wait_for,
thread_t *departed,
void **status
);
where the parameters have the following meaning:
wait_for-
The ID of the thread of interest,
that is, the non-detached thread whose termination the caller will await.
A (thread_t)0 indicates interest in the next non-detached thread to
terminate (or one that has already terminated, but has not been joined),
whatever its ID happens to be.
departed-
thr_join(3thread)
will deposit the thread ID of the
terminated thread at this address.
status-
thr_join(3thread)
will deposit at this address the
value given as an argument by the terminated thread
when it called
thr_exit(3thread).
That value should be the address
at which the terminated thread left its return value (exit status).
If the thread of interest has already terminated,
thr_join(3thread)
will return immediately; otherwise, the calling thread will block.
If there is more than one thread waiting for the termination of
some particular thread:
-
The thread of interest will be joined to only one of the waiting threads.
The choice is not predictable.
-
All other waiting threads will return with the ESRCH error code.
If a thread receives a catchable
signal while blocked in
thr_join(3thread):
-
The signal is handled.
-
The
thr_join(3thread)
function is transparently restarted.
NOTE:
This is analogous to the
``autorestart option'' for blocking system calls.
(See the description of the SA_RESTART flag
for the
sigaction(2)
system call.)
The resources of a non-detached thread
(for example, a stack allocated by the Threads Library)
will not be fully recovered by the Threads Library
until some other thread has called
thr_join(3thread)
and received
the terminated thread's exit status.
NOTE:
Beware of lingering zombies! Failing to recover the memory
and resources associated with terminated threads can have
a negative impact on performance.
Detached threads
If the programmer knows at thread creation time that no other thread will
use
thr_join(3thread)
to wait for the new thread,
the THR_DETACHED flag to
thr_create(3thread)
should be used.
When a ``detached thread'' terminates, its resources may be recovered
immediately.
In fact, it is not valid to use
thr_join(3thread)
on a detached thread.
By default, new threads are not detached threads.
Next topic:
Thread-specific data
Previous topic:
Termination of the process
© 2004 The SCO Group, Inc. All rights reserved.
UnixWare 7 Release 7.1.4 - 27 April 2004