2 .\" Copyright (c) 2005 by Michael Kerrisk <mtk.manpages@gmail.com>
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.
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.
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
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
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
24 .TH PTHREADS 7 2008-05-29 "Linux" "Linux Programmer's Manual"
26 pthreads \- POSIX threads
28 POSIX.1 specifies a set of interfaces (functions, header files) for
29 threaded programming commonly known as POSIX threads, or Pthreads.
30 A single process can contain multiple threads,
31 all of which are executing the same program.
32 These threads share the same global memory (data and heap segments),
33 but each thread has its own stack (automatic variables).
35 POSIX.1 also requires that threads share a range of other attributes
36 (i.e., these attributes are process-wide rather than per-thread):
42 process group ID and session ID
55 file mode creation mask
67 .RB ( timer_create (3))
70 .RB ( setpriority (2))
75 measurements of the consumption of CPU time
80 As well as the stack, POSIX.1 specifies that various other
81 attributes are distinct for each thread, including:
88 .RB ( pthread_sigmask (3))
94 alternate signal stack
95 .RB ( sigaltstack (2))
97 real-time scheduling policy and priority
98 .RB ( sched_setscheduler (2)
100 .BR sched_setparam (2))
102 The following Linux-specific features are also per-thread:
105 .BR capabilities (7))
108 .RB ( sched_setaffinity (2))
109 .SS "Thread-safe functions"
110 A thread-safe function is one that can be safely
111 (i.e., it will deliver the same results regardless of whether it is)
112 called from multiple threads at the same time.
114 POSIX.1-2001 requires that all functions specified in the standard
115 shall 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().
126 ctermid() if passed a non-NULL argument
204 tmpnam() if passed a non-NULL argument
207 wcrtomb() if its final argument is NULL
208 wcsrtombs() if its final argument is NULL
213 .SS "Compiling on Linux"
214 On Linux, programs that use the Pthreads API should be compiled using
216 .SS "Linux Implementations of POSIX Threads"
217 Over time, two threading implementations have been provided by
218 the GNU C library on Linux:
221 This is the original Pthreads implementation.
222 Since glibc 2.4, this implementation is no longer supported.
224 .BR NPTL " (Native POSIX Threads Library)"
225 This is the modern Pthreads implementation.
226 By comparison with LinuxThreads, NPTL provides closer conformance to
227 the requirements of the POSIX.1 specification and better performance
228 when creating large numbers of threads.
229 NPTL is available since glibc 2.3.2,
230 and requires features that are present in the Linux 2.6 kernel.
232 Both of these are so-called 1:1 implementations, meaning that each
233 thread maps to a kernel scheduling entity.
234 Both threading implementations employ the Linux
237 In NPTL, thread synchronization primitives (mutexes,
238 thread joining, etc.) are implemented using the Linux
242 The notable features of this implementation are the following:
244 In addition to the main (initial) thread,
245 and the threads that the program creates using
246 .BR pthread_create (3),
247 the implementation creates a "manager" thread.
248 This thread handles thread creation and termination.
249 (Problems can result if this thread is inadvertently killed.)
251 Signals are used internally by the implementation.
252 On Linux 2.2 and later, the first three real-time signals are used.
253 On older Linux kernels,
258 Applications must avoid the use of whichever set of signals is
259 employed by the implementation.
261 Threads do not share process IDs.
262 (In effect, LinuxThreads threads are implemented as processes which share
263 more information than usual, but which do not share a common process ID.)
264 LinuxThreads threads (including the manager thread)
265 are visible as separate processes using
268 The LinuxThreads implementation deviates from the POSIX.1
269 specification in a number of ways, including the following:
273 return a different value in each thread.
277 in threads other than the main thread return the process ID of the
278 manager thread; instead
280 in these threads should return the same value as
284 When one thread creates a new child process using
286 any thread should be able to
289 However, the implementation only allows the thread that
296 all other threads are terminated (as required by POSIX.1).
297 However, the resulting process has the same PID as the thread that called
299 it should have the same PID as the main thread.
301 Threads do not share user and group IDs.
302 This can cause complications with set-user-ID programs and
303 can cause failures in Pthreads functions if an application
304 changes its credentials using
308 Threads do not share a common session ID and process group ID.
310 Threads do not share record locks created using
313 The information returned by
317 is per-thread rather than process-wide.
319 Threads do not share semaphore undo values (see
322 Threads do not share interval timers.
324 Threads do not share a common nice value.
326 POSIX.1 distinguishes the notions of signals that are directed
327 to the process as a whole and signals that are directed to individual
329 According to POSIX.1, a process-directed signal (sent using
331 for example) should be handled by a single,
332 arbitrarily selected thread within the process.
333 LinuxThreads does not support the notion of process-directed signals:
334 signals may only be sent to specific threads.
336 Threads have distinct alternate signal stack settings.
337 However, a new thread's alternate signal stack settings
338 are copied from the thread that created it, so that
339 the threads initially share an alternate signal stack.
340 (A new thread should start with no alternate signal stack defined.
341 If two threads handle signals on their shared alternate signal
342 stack at the same time, unpredictable program failures are
345 With NPTL, all of the threads in a process are placed
346 in the same thread group;
347 all members of a thread groups share the same PID.
348 NPTL does not employ a manager thread.
349 NPTL makes internal use of the first two real-time signals;
350 these signals cannot be used in applications.
352 NPTL still has at least one non-conformance with POSIX.1:
354 Threads do not share a common nice value.
355 .\" FIXME . bug report filed for NPTL nice non-conformance
356 .\" http://bugzilla.kernel.org/show_bug.cgi?id=6258
358 Some NPTL non-conformances only occur with older kernels:
360 The information returned by
364 is per-thread rather than process-wide (fixed in kernel 2.6.9).
366 Threads do not share resource limits (fixed in kernel 2.6.10).
368 Threads do not share interval timers (fixed in kernel 2.6.12).
370 Only the main thread is permitted to start a new session using
372 (fixed in kernel 2.6.16).
374 Only the main thread is permitted to make the process into a
375 process group leader using
377 (fixed in kernel 2.6.16).
379 Threads have distinct alternate signal stack settings.
380 However, a new thread's alternate signal stack settings
381 are copied from the thread that created it, so that
382 the threads initially share an alternate signal stack
383 (fixed in kernel 2.6.16).
385 Note the following further points about the NPTL implementation:
387 If the stack size soft resource limit (see the description of
391 is set to a value other than
393 then this value defines the default stack size for new threads.
394 To be effective, this limit must be set before the program
395 is executed, perhaps using the
397 shell built-in command
398 .RI ( "limit stacksize"
400 .SS "Determining the Threading Implementation"
401 Since glibc 2.3.2, the
403 command can be used to determine
404 the system's threading implementation, for example:
408 bash$ getconf GNU_LIBPTHREAD_VERSION
413 With older glibc versions, a command such as the following should
414 be sufficient to determine the default threading implementation:
418 bash$ $( ldd /bin/ls | grep libc.so | awk \(aq{print $3}\(aq ) | \\
419 egrep \-i \(aqthreads|nptl\(aq
420 Native POSIX Threads Library by Ulrich Drepper et al
423 .SS "Selecting the Threading Implementation: LD_ASSUME_KERNEL"
424 On systems with a glibc that supports both LinuxThreads and NPTL
425 (i.e., glibc 2.3.\fIx\fP), the
427 environment variable can be used to override
428 the dynamic linker's default choice of threading implementation.
429 This variable tells the dynamic linker to assume that it is
430 running on top of a particular kernel version.
431 By specifying a kernel version that does not
432 provide the support required by NPTL, we can force the use
434 (The most likely reason for doing this is to run a
435 (broken) application that depends on some non-conformant behavior
441 bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \\
442 awk \(aq{print $3}\(aq ) | egrep \-i \(aqthreads|ntpl\(aq
443 linuxthreads-0.10 by Xavier Leroy
451 and various Pthreads manual pages, for example:
452 .BR pthread_atfork (3),
453 .BR pthread_cleanup_push (3),
454 .BR pthread_cond_signal (3),
455 .BR pthread_cond_wait (3),
456 .BR pthread_create (3),
457 .BR pthread_detach (3),
458 .BR pthread_equal (3),
459 .BR pthread_exit (3),
460 .BR pthread_key_create (3),
461 .BR pthread_kill (3),
462 .BR pthread_mutex_lock (3),
463 .BR pthread_mutex_unlock (3),
464 .BR pthread_once (3),
465 .BR pthread_setcancelstate (3),
466 .BR pthread_setcanceltype (3),
467 .BR pthread_setspecific (3),
468 .BR pthread_sigmask (3),
470 .BR pthread_testcancel (3).