1PTHREADS(7) Linux Programmer's Manual PTHREADS(7)
2
3
4
6 pthreads - POSIX threads
7
9 POSIX.1 specifies a set of interfaces (functions, header files) for
10 threaded programming commonly known as POSIX threads, or Pthreads. A
11 single process can contain multiple threads, all of which are executing
12 the same program. These threads share the same global memory (data and
13 heap segments), but each thread has its own stack (automatic vari‐
14 ables).
15
16 POSIX.1 also requires that threads share a range of other attributes
17 (i.e., these attributes are process-wide rather than per-thread):
18
19 - process ID
20
21 - parent process ID
22
23 - process group ID and session ID
24
25 - controlling terminal
26
27 - user and group IDs
28
29 - open file descriptors
30
31 - record locks (see fcntl(2))
32
33 - signal dispositions
34
35 - file mode creation mask (umask(2))
36
37 - current directory (chdir(2)) and root directory (chroot(2))
38
39 - interval timers (setitimer(2)) and POSIX timers (timer_create(2))
40
41 - nice value (setpriority(2))
42
43 - resource limits (setrlimit(2))
44
45 - measurements of the consumption of CPU time (times(2)) and resources
46 (getrusage(2))
47
48 As well as the stack, POSIX.1 specifies that various other attributes
49 are distinct for each thread, including:
50
51 - thread ID (the pthread_t data type)
52
53 - signal mask (pthread_sigmask(3))
54
55 - the errno variable
56
57 - alternate signal stack (sigaltstack(2))
58
59 - real-time scheduling policy and priority (sched(7))
60
61 The following Linux-specific features are also per-thread:
62
63 - capabilities (see capabilities(7))
64
65 - CPU affinity (sched_setaffinity(2))
66
67 Pthreads function return values
68 Most pthreads functions return 0 on success, and an error number on
69 failure. The error numbers that can be returned have the same meaning
70 as the error numbers returned in errno by conventional system calls and
71 C library functions. Note that the pthreads functions do not set er‐
72 rno. For each of the pthreads functions that can return an error,
73 POSIX.1-2001 specifies that the function can never fail with the error
74 EINTR.
75
76 Thread IDs
77 Each of the threads in a process has a unique thread identifier (stored
78 in the type pthread_t). This identifier is returned to the caller of
79 pthread_create(3), and a thread can obtain its own thread identifier
80 using pthread_self(3).
81
82 Thread IDs are guaranteed to be unique only within a process. (In all
83 pthreads functions that accept a thread ID as an argument, that ID by
84 definition refers to a thread in the same process as the caller.)
85
86 The system may reuse a thread ID after a terminated thread has been
87 joined, or a detached thread has terminated. POSIX says: "If an appli‐
88 cation attempts to use a thread ID whose lifetime has ended, the behav‐
89 ior is undefined."
90
91 Thread-safe functions
92 A thread-safe function is one that can be safely (i.e., it will deliver
93 the same results regardless of whether it is) called from multiple
94 threads at the same time.
95
96 POSIX.1-2001 and POSIX.1-2008 require that all functions specified in
97 the standard shall be thread-safe, except for the following functions:
98
99 asctime()
100 basename()
101 catgets()
102 crypt()
103 ctermid() if passed a non-NULL argument
104 ctime()
105 dbm_clearerr()
106 dbm_close()
107 dbm_delete()
108 dbm_error()
109 dbm_fetch()
110 dbm_firstkey()
111 dbm_nextkey()
112 dbm_open()
113 dbm_store()
114 dirname()
115 dlerror()
116 drand48()
117 ecvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
118 encrypt()
119 endgrent()
120 endpwent()
121 endutxent()
122 fcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
123 ftw()
124 gcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
125 getc_unlocked()
126 getchar_unlocked()
127 getdate()
128 getenv()
129 getgrent()
130 getgrgid()
131 getgrnam()
132 gethostbyaddr() [POSIX.1-2001 only (function removed in
133 POSIX.1-2008)]
134 gethostbyname() [POSIX.1-2001 only (function removed in
135 POSIX.1-2008)]
136 gethostent()
137 getlogin()
138 getnetbyaddr()
139 getnetbyname()
140 getnetent()
141 getopt()
142 getprotobyname()
143 getprotobynumber()
144 getprotoent()
145 getpwent()
146 getpwnam()
1