| 1 |
'\" t |
| 2 |
.\" Copyright, the authors of the Linux man-pages project |
| 3 |
.\" |
| 4 |
.\" SPDX-License-Identifier: Linux-man-pages-copyleft |
| 5 |
.\" |
| 6 |
.TH pthread_detach 3 2025-05-17 "Linux man-pages 6.18" |
| 7 |
.SH NAME |
| 8 |
pthread_detach \- detach a thread |
| 9 |
.SH LIBRARY |
| 10 |
POSIX threads library |
| 11 |
.RI ( libpthread ,\~ \-lpthread ) |
| 12 |
.SH SYNOPSIS |
| 13 |
.nf |
| 14 |
.B #include <pthread.h> |
| 15 |
.P |
| 16 |
.BI "int pthread_detach(pthread_t " thread ); |
| 17 |
.fi |
| 18 |
.SH DESCRIPTION |
| 19 |
The |
| 20 |
.BR pthread_detach () |
| 21 |
function marks the thread identified by |
| 22 |
.I thread |
| 23 |
as detached. |
| 24 |
When a detached thread terminates, |
| 25 |
its resources are automatically released back to the system without |
| 26 |
the need for another thread to join with the terminated thread. |
| 27 |
.P |
| 28 |
Attempting to detach an already detached thread results |
| 29 |
in unspecified behavior. |
| 30 |
.SH RETURN VALUE |
| 31 |
On success, |
| 32 |
.BR pthread_detach () |
| 33 |
returns 0; |
| 34 |
on error, it returns an error number. |
| 35 |
.SH ERRORS |
| 36 |
.TP |
| 37 |
.B EINVAL |
| 38 |
.I thread |
| 39 |
is not a joinable thread. |
| 40 |
.TP |
| 41 |
.B ESRCH |
| 42 |
No thread with the ID |
| 43 |
.I thread |
| 44 |
could be found. |
| 45 |
.SH ATTRIBUTES |
| 46 |
For an explanation of the terms used in this section, see |
| 47 |
.BR attributes (7). |
| 48 |
.TS |
| 49 |
allbox; |
| 50 |
lbx lb lb |
| 51 |
l l l. |
| 52 |
Interface Attribute Value |
| 53 |
T{ |
| 54 |
.na |
| 55 |
.nh |
| 56 |
.BR pthread_detach () |
| 57 |
T} Thread safety MT-Safe |
| 58 |
.TE |
| 59 |
.SH STANDARDS |
| 60 |
POSIX.1-2008. |
| 61 |
.SH HISTORY |
| 62 |
POSIX.1-2001. |
| 63 |
.SH NOTES |
| 64 |
Once a thread has been detached, it can't be joined with |
| 65 |
.BR pthread_join (3) |
| 66 |
or be made joinable again. |
| 67 |
.P |
| 68 |
A new thread can be created in a detached state using |
| 69 |
.BR pthread_attr_setdetachstate (3) |
| 70 |
to set the detached attribute of the |
| 71 |
.I attr |
| 72 |
argument of |
| 73 |
.BR pthread_create (3). |
| 74 |
.P |
| 75 |
The detached attribute merely determines the behavior of the system |
| 76 |
when the thread terminates; |
| 77 |
it does not prevent the thread from being terminated |
| 78 |
if the process terminates using |
| 79 |
.BR exit (3) |
| 80 |
(or equivalently, if the main thread returns). |
| 81 |
.P |
| 82 |
Either |
| 83 |
.BR pthread_join (3) |
| 84 |
or |
| 85 |
.BR pthread_detach () |
| 86 |
should be called for each thread that an application creates, |
| 87 |
so that system resources for the thread can be released. |
| 88 |
(But note that the resources of any threads for which one of these |
| 89 |
actions has not been done will be freed when the process terminates.) |
| 90 |
.SH EXAMPLES |
| 91 |
The following statement detaches the calling thread: |
| 92 |
.P |
| 93 |
.in +4n |
| 94 |
.EX |
| 95 |
pthread_detach(pthread_self()); |
| 96 |
.EE |
| 97 |
.in |
| 98 |
.SH SEE ALSO |
| 99 |
.BR pthread_attr_setdetachstate (3), |
| 100 |
.BR pthread_cancel (3), |
| 101 |
.BR pthread_create (3), |
| 102 |
.BR pthread_exit (3), |
| 103 |
.BR pthread_join (3), |
| 104 |
.BR pthreads (7) |