1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
'\" t
.\" Copyright (c) 2005 by Michael Kerrisk <mtk.manpages@gmail.com>
.\"
.\" Permission is granted to make and distribute verbatim copies of this
.\" manual provided the copyright notice and this permission notice are
.\" preserved on all copies.
.\"
.\" Permission is granted to copy and distribute modified versions of this
.\" manual under the conditions for verbatim copying, provided that the
.\" entire resulting derived work is distributed under the terms of a
.\" permission notice identical to this one.
.\"
.\" Since the Linux kernel and libraries are constantly changing, this
.\" manual page may be incorrect or out-of-date. The author(s) assume no
.\" responsibility for errors or omissions, or for damages resulting from
.\" the use of the information contained herein. The author(s) may not
.\" have taken the same level of care in the production of this manual,
.\" which is licensed free of charge, as they might when working
.\" professionally.
.\"
.\" Formatted or processed versions of this manual, if unaccompanied by
.\" the source, must acknowledge the copyright and authors of this work.
.\"
.TH PTHREADS 7 2008-08-24 "Linux" "Linux Programmer's Manual"
.SH NAME
pthreads \- POSIX threads
.SH DESCRIPTION
POSIX.1 specifies a set of interfaces (functions, header files) for
threaded programming commonly known as POSIX threads, or Pthreads.
A single process can contain multiple threads,
all of which are executing the same program.
These threads share the same global memory (data and heap segments),
but each thread has its own stack (automatic variables).
POSIX.1 also requires that threads share a range of other attributes
(i.e., these attributes are process-wide rather than per-thread):
.IP \- 3
process ID
.IP \- 3
parent process ID
.IP \- 3
process group ID and session ID
.IP \- 3
controlling terminal
.IP \- 3
user and group IDs
.IP \- 3
open file descriptors
.IP \- 3
record locks (see
.BR fcntl (2))
.IP \- 3
signal dispositions
.IP \- 3
file mode creation mask
.RB ( umask (2))
.IP \- 3
current directory
.RB ( chdir (2))
and
root directory
.RB ( chroot (2))
.IP \- 3
interval timers
.RB ( setitimer (2))
and POSIX timers
.RB ( timer_create (3))
.IP \- 3
nice value
.RB ( setpriority (2))
.IP \- 3
resource limits
.RB ( setrlimit (2))
.IP \- 3
measurements of the consumption of CPU time
.RB ( times (2))
and resources
.RB ( getrusage (2))
.PP
As well as the stack, POSIX.1 specifies that various other
attributes are distinct for each thread, including:
.IP \- 3
thread ID (the
.I pthread_t
data type)
.IP \- 3
signal mask
.RB ( pthread_sigmask (3))
.IP \- 3
the
.I errno
variable
.IP \- 3
alternate signal stack
.RB ( sigaltstack (2))
.IP \- 3
real-time scheduling policy and priority
.RB ( sched_setscheduler (2)
and
.BR sched_setparam (2))
.PP
The following Linux-specific features are also per-thread:
.IP \- 3
capabilities (see
.BR capabilities (7))
.IP \- 3
CPU affinity
.RB ( sched_setaffinity (2))
.SS "Thread-safe functions"
A thread-safe function is one that can be safely
(i.e., it will deliver the same results regardless of whether it is)
called from multiple threads at the same time.
POSIX.1-2001 requires that all functions specified in the standard
shall be thread-safe, except for the following functions:
.in +4n
.nf
asctime()
basename()
catgets()
crypt()
ctermid() if passed a non-NULL argument
ctime()
dbm_clearerr()
dbm_close()
dbm_delete()
dbm_error()
dbm_fetch()
dbm_firstkey()
dbm_nextkey()
dbm_open()
dbm_store()
dirname()
dlerror()
drand48()
ecvt()
encrypt()
endgrent()
endpwent()
endutxent()
fcvt()
ftw()
gcvt()
getc_unlocked()
getchar_unlocked()
getdate()
getenv()
getgrent()
getgrgid()
getgrnam()
gethostbyaddr()
gethostbyname()
gethostent()
getlogin()
getnetbyaddr()
getnetbyname()
getnetent()
getopt()
getprotobyname()
getprotobynumber()
getprotoent()
getpwent()
getpwnam()
getpwuid()
getservbyname()
getservbyport()
getservent()
getutxent()
getutxid()
getutxline()
gmtime()
hcreate()
hdestroy()
hsearch()
inet_ntoa()
l64a()
lgamma()
lgammaf()
lgammal()
localeconv()
localtime()
lrand48()
mrand48()
nftw()
nl_langinfo()
ptsname()
putc_unlocked()
putchar_unlocked()
putenv()
pututxline()
rand()
readdir()
setenv()
setgrent()
setkey()
setpwent()
setutxent()
strerror()
strtok()
tmpnam() if passed a non-NULL argument
ttyname()
unsetenv()
wcrtomb() if its final argument is NULL
wcsrtombs() if its final argument is NULL
wcstombs()
wctomb()
.fi
.in
.PP
POSIX.1-2008 removes ecvt(), fcvt(), gcvt(), gethostbyname(),
and gethostbyaddr() from the above list
(because those functions are removed from the standard), and adds
strerror() and system().
.SS "Compiling on Linux"
On Linux, programs that use the Pthreads API should be compiled using
.IR "cc \-pthread" .
.SS "Linux Implementations of POSIX Threads"
Over time, two threading implementations have been provided by
the GNU C library on Linux:
.TP
.B LinuxThreads
This is the original Pthreads implementation.
Since glibc 2.4, this implementation is no longer supported.
.TP
.BR NPTL " (Native POSIX Threads Library)"
This is the modern Pthreads implementation.
By comparison with LinuxThreads, NPTL provides closer conformance to
the requirements of the POSIX.1 specification and better performance
when creating large numbers of threads.
NPTL is available since glibc 2.3.2,
and requires features that are present in the Linux 2.6 kernel.
.PP
Both of these are so-called 1:1 implementations, meaning that each
thread maps to a kernel scheduling entity.
Both threading implementations employ the Linux
.BR clone (2)
system call.
In NPTL, thread synchronization primitives (mutexes,
thread joining, etc.) are implemented using the Linux
.BR futex (2)
system call.
.SS LinuxThreads
The notable features of this implementation are the following:
.IP \- 3
In addition to the main (initial) thread,
and the threads that the program creates using
.BR pthread_create (3),
the implementation creates a "manager" thread.
This thread handles thread creation and termination.
(Problems can result if this thread is inadvertently killed.)
.IP \- 3
Signals are used internally by the implementation.
On Linux 2.2 and later, the first three real-time signals are used.
On older Linux kernels,
.B SIGUSR1
and
.B SIGUSR2
are used.
Applications must avoid the use of whichever set of signals is
employed by the implementation.
.IP \- 3
Threads do not share process IDs.
(In effect, LinuxThreads threads are implemented as processes which share
more information than usual, but which do not share a common process ID.)
LinuxThreads threads (including the manager thread)
are visible as separate processes using
.BR ps (1).
.PP
The LinuxThreads implementation deviates from the POSIX.1
specification in a number of ways, including the following:
.IP \- 3
Calls to
.BR getpid (2)
return a different value in each thread.
.IP \- 3
Calls to
.BR getppid (2)
in threads other than the main thread return the process ID of the
manager thread; instead
.BR getppid (2)
in these threads should return the same value as
.BR getppid (2)
in the main thread.
.IP \- 3
When one thread creates a new child process using
.BR fork (2),
any thread should be able to
.BR wait (2)
on the child.
However, the implementation only allows the thread that
created the child to
.BR wait (2)
on it.
.IP \- 3
When a thread calls
.BR execve (2),
all other threads are terminated (as required by POSIX.1).
However, the resulting process has the same PID as the thread that called
.BR execve (2):
it should have the same PID as the main thread.
.IP \- 3
Threads do not share user and group IDs.
This can cause complications with set-user-ID programs and
can cause failures in Pthreads functions if an application
changes its credentials using
.BR seteuid (2)
or similar.
.IP \- 3
Threads do not share a common session ID and process group ID.
.IP \- 3
Threads do not share record locks created using
.BR fcntl (2).
.IP \- 3
The information returned by
.BR times (2)
and
.BR getrusage (2)
is per-thread rather than process-wide.
.IP \- 3
Threads do not share semaphore undo values (see
.BR semop (2)).
.IP \- 3
Threads do not share interval timers.
.IP \- 3
Threads do not share a common nice value.
.IP \- 3
POSIX.1 distinguishes the notions of signals that are directed
to the process as a whole and signals that are directed to individual
threads.
According to POSIX.1, a process-directed signal (sent using
.BR kill (2),
for example) should be handled by a single,
arbitrarily selected thread within the process.
LinuxThreads does not support the notion of process-directed signals:
signals may only be sent to specific threads.
.IP \- 3
Threads have distinct alternate signal stack settings.
However, a new thread's alternate signal stack settings
are copied from the thread that created it, so that
the threads initially share an alternate signal stack.
(A new thread should start with no alternate signal stack defined.
If two threads handle signals on their shared alternate signal
stack at the same time, unpredictable program failures are
likely to occur.)
.SS NPTL
With NPTL, all of the threads in a process are placed
in the same thread group;
all members of a thread groups share the same PID.
NPTL does not employ a manager thread.
NPTL makes internal use of the first two real-time signals;
these signals cannot be used in applications.
NPTL still has at least one non-conformance with POSIX.1:
.IP \- 3
Threads do not share a common nice value.
.\" FIXME . bug report filed for NPTL nice non-conformance
.\" http://bugzilla.kernel.org/show_bug.cgi?id=6258
.\" Sep 08: there is a patch by Denys Vlasenko to address this
.\" "make setpriority POSIX compliant; introduce PRIO_THREAD extension"
.\" Monitor this to see if it makes it into mainline.
.PP
Some NPTL non-conformances only occur with older kernels:
.IP \- 3
The information returned by
.BR times (2)
and
.BR getrusage (2)
is per-thread rather than process-wide (fixed in kernel 2.6.9).
.IP \- 3
Threads do not share resource limits (fixed in kernel 2.6.10).
.IP \- 3
Threads do not share interval timers (fixed in kernel 2.6.12).
.IP \- 3
Only the main thread is permitted to start a new session using
.BR setsid (2)
(fixed in kernel 2.6.16).
.IP \- 3
Only the main thread is permitted to make the process into a
process group leader using
.BR setpgid (2)
(fixed in kernel 2.6.16).
.IP \- 3
Threads have distinct alternate signal stack settings.
However, a new thread's alternate signal stack settings
are copied from the thread that created it, so that
the threads initially share an alternate signal stack
(fixed in kernel 2.6.16).
.PP
Note the following further points about the NPTL implementation:
.IP \- 3
If the stack size soft resource limit (see the description of
.B RLIMIT_STACK
in
.BR setrlimit (2))
is set to a value other than
.IR unlimited ,
then this value defines the default stack size for new threads.
To be effective, this limit must be set before the program
is executed, perhaps using the
.I ulimit -s
shell built-in command
.RI ( "limit stacksize"
in the C shell).
.SS "Determining the Threading Implementation"
Since glibc 2.3.2, the
.BR getconf (1)
command can be used to determine
the system's threading implementation, for example:
.nf
.in +4n
bash$ getconf GNU_LIBPTHREAD_VERSION
NPTL 2.3.4
.in
.fi
.PP
With older glibc versions, a command such as the following should
be sufficient to determine the default threading implementation:
.nf
.in +4n
bash$ $( ldd /bin/ls | grep libc.so | awk \(aq{print $3}\(aq ) | \\
egrep \-i \(aqthreads|nptl\(aq
Native POSIX Threads Library by Ulrich Drepper et al
.in
.fi
.SS "Selecting the Threading Implementation: LD_ASSUME_KERNEL"
On systems with a glibc that supports both LinuxThreads and NPTL
(i.e., glibc 2.3.\fIx\fP), the
.B LD_ASSUME_KERNEL
environment variable can be used to override
the dynamic linker's default choice of threading implementation.
This variable tells the dynamic linker to assume that it is
running on top of a particular kernel version.
By specifying a kernel version that does not
provide the support required by NPTL, we can force the use
of LinuxThreads.
(The most likely reason for doing this is to run a
(broken) application that depends on some non-conformant behavior
in LinuxThreads.)
For example:
.nf
.in +4n
bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \\
awk \(aq{print $3}\(aq ) | egrep \-i \(aqthreads|ntpl\(aq
linuxthreads-0.10 by Xavier Leroy
.in
.fi
.SH "SEE ALSO"
.BR clone (2),
.BR futex (2),
.BR gettid (2),
.BR proc (5),
.BR futex (7),
.br
and various Pthreads manual pages, for example:
.BR pthread_atfork (3),
.BR pthread_cleanup_push (3),
.BR pthread_cond_signal (3),
.BR pthread_cond_wait (3),
.BR pthread_create (3),
.BR pthread_detach (3),
.BR pthread_equal (3),
.BR pthread_exit (3),
.BR pthread_key_create (3),
.BR pthread_kill (3),
.BR pthread_mutex_lock (3),
.BR pthread_mutex_unlock (3),
.BR pthread_once (3),
.BR pthread_setcancelstate (3),
.BR pthread_setcanceltype (3),
.BR pthread_setspecific (3),
.BR pthread_sigmask (3),
and
.BR pthread_testcancel (3)
|