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