Professional Multicore Programming: Design and Implementation for C++ Developers
by Cameron Hughes, Tracey Hughes
Name
NAME
pthread_detach — detach a thread
SYNOPSIS
THR #include <pthread.h>
int pthread_detach(pthread_t thread);DESCRIPTION
The pthread_detach() function shall indicate to the implementation that storage for the thread thread can be reclaimed when that thread terminates. If thread has not terminated, pthread_detach() shall not cause it to terminate. The effect of multiple pthread_detach() calls on the same target thread is unspecified.
RETURN VALUE
If the call succeeds, pthread_detach() shall return 0; otherwise, an error number shall be returned to indicate the error.
ERRORS
The pthread_detach() function shall fail if:
[EINVAL] | The implementation has detected that the value specified by thread does not refer to a joinable thread. |
[ESRCH] | No thread could be found corresponding to that specified by the given thread ID. |
The pthread_detach() function shall not return an error code of [EINTR].
EXAMPLES
None.
APPLICATION USAGE
None.
RATIONALE
The pthread_join() or pthread_detach() functions should eventually be called for every thread that is created so that storage associated with the thread may be reclaimed.
It has been suggested that a "detach" function is not necessary; the detachstate thread creation attribute is sufficient, since a thread need never be dynamically detached. However, need arises in at least two cases:
In a cancelation handler for a pthread_join() it is nearly essential to have a pthread_detach() function in order to detach the thread on which pthread_join() was waiting. Without it, ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access