1pthread_detach(3)          Library Functions Manual          pthread_detach(3)
2
3
4

NAME

6       pthread_detach - detach a thread
7

LIBRARY

9       POSIX threads library (libpthread, -lpthread)
10

SYNOPSIS

12       #include <pthread.h>
13
14       int pthread_detach(pthread_t thread);
15

DESCRIPTION

17       The  pthread_detach() function marks the thread identified by thread as
18       detached.  When a detached thread terminates, its resources  are  auto‐
19       matically  released  back  to  the  system without the need for another
20       thread to join with the terminated thread.
21
22       Attempting to detach an already detached thread results in  unspecified
23       behavior.
24

RETURN VALUE

26       On  success,  pthread_detach() returns 0; on error, it returns an error
27       number.
28

ERRORS

30       EINVAL thread is not a joinable thread.
31
32       ESRCH  No thread with the ID thread could be found.
33

ATTRIBUTES

35       For an  explanation  of  the  terms  used  in  this  section,  see  at‐
36       tributes(7).
37
38       ┌────────────────────────────────────────────┬───────────────┬─────────┐
39Interface                                   Attribute     Value   
40       ├────────────────────────────────────────────┼───────────────┼─────────┤
41pthread_detach()                            │ Thread safety │ MT-Safe │
42       └────────────────────────────────────────────┴───────────────┴─────────┘
43

STANDARDS

45       POSIX.1-2008.
46

HISTORY

48       POSIX.1-2001.
49

NOTES

51       Once   a   thread   has   been   detached,  it  can't  be  joined  with
52       pthread_join(3) or be made joinable again.
53
54       A  new  thread   can   be   created   in   a   detached   state   using
55       pthread_attr_setdetachstate(3)  to  set  the  detached attribute of the
56       attr argument of pthread_create(3).
57
58       The detached attribute merely determines the  behavior  of  the  system
59       when  the  thread terminates; it does not prevent the thread from being
60       terminated if the process terminates using exit(3) (or equivalently, if
61       the main thread returns).
62
63       Either  pthread_join(3)  or  pthread_detach() should be called for each
64       thread that an application creates, so that system  resources  for  the
65       thread  can  be  released.  (But note that the resources of any threads
66       for which one of these actions has not been done will be freed when the
67       process terminates.)
68

EXAMPLES

70       The following statement detaches the calling thread:
71
72           pthread_detach(pthread_self());
73

SEE ALSO

75       pthread_attr_setdetachstate(3),  pthread_cancel(3),  pthread_create(3),
76       pthread_exit(3), pthread_join(3), pthreads(7)
77
78
79
80Linux man-pages 6.05              2023-07-20                 pthread_detach(3)
Impressum