2 .\" Copyright (c) 2005 by Michael Kerrisk <mtk.manpages@gmail.com>
4 .\" %%%LICENSE_START(VERBATIM)
5 .\" Permission is granted to make and distribute verbatim copies of this
6 .\" manual provided the copyright notice and this permission notice are
7 .\" preserved on all copies.
9 .\" Permission is granted to copy and distribute modified versions of this
10 .\" manual under the conditions for verbatim copying, provided that the
11 .\" entire resulting derived work is distributed under the terms of a
12 .\" permission notice identical to this one.
14 .\" Since the Linux kernel and libraries are constantly changing, this
15 .\" manual page may be incorrect or out-of-date. The author(s) assume no
16 .\" responsibility for errors or omissions, or for damages resulting from
17 .\" the use of the information contained herein. The author(s) may not
18 .\" have taken the same level of care in the production of this manual,
19 .\" which is licensed free of charge, as they might when working
22 .\" Formatted or processed versions of this manual, if unaccompanied by
23 .\" the source, must acknowledge the copyright and authors of this work.
26 .TH PTHREADS 7 2014-05-21 "Linux" "Linux Programmer's Manual"
28 pthreads \- POSIX threads
30 POSIX.1 specifies a set of interfaces (functions, header files) for
31 threaded programming commonly known as POSIX threads, or Pthreads.
32 A single process can contain multiple threads,
33 all of which are executing the same program.
34 These threads share the same global memory (data and heap segments),
35 but each thread has its own stack (automatic variables).
37 POSIX.1 also requires that threads share a range of other attributes
38 (i.e., these attributes are process-wide rather than per-thread):
44 process group ID and session ID
57 file mode creation mask
69 .RB ( timer_create (2))
72 .RB ( setpriority (2))
77 measurements of the consumption of CPU time
82 As well as the stack, POSIX.1 specifies that various other
83 attributes are distinct for each thread, including:
90 .RB ( pthread_sigmask (3))
96 alternate signal stack
97 .RB ( sigaltstack (2))
99 real-time scheduling policy and priority
102 The following Linux-specific features are also per-thread:
105 .BR capabilities (7))
108 .RB ( sched_setaffinity (2))
109 .SS Pthreads function return values
110 Most pthreads functions return 0 on success, and an error number of failure.
111 Note that the pthreads functions do not set
113 For each of the pthreads functions that can return an error,
114 POSIX.1-2001 specifies that the function can never fail with the error
117 Each of the threads in a process has a unique thread identifier
120 This identifier is returned to the caller of
121 .BR pthread_create (3),
122 and a thread can obtain its own thread identifier using
123 .BR pthread_self (3).
124 Thread IDs are guaranteed to be unique only within a process.
125 A thread ID may be reused after a terminated thread has been joined,
126 or a detached thread has terminated.
127 In all pthreads functions that accept a thread ID as an argument,
128 that ID by definition refers to a thread in
129 the same process as the caller.
130 .SS Thread-safe functions
131 A thread-safe function is one that can be safely
132 (i.e., it will deliver the same results regardless of whether it is)
133 called from multiple threads at the same time.
135 POSIX.1-2001 and POSIX.1-2008 require that all functions specified
136 in the standard shall be thread-safe,
137 except for the following functions:
145 ctermid() if passed a non-NULL argument
159 ecvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
164 fcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
166 gcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
174 gethostbyaddr() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
175 gethostbyname() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
222 strsignal() [Added in POSIX.1-2008]
224 system() [Added in POSIX.1-2008]
225 tmpnam() if passed a non-NULL argument
228 wcrtomb() if its final argument is NULL
229 wcsrtombs() if its final argument is NULL
234 .SS Async-cancel-safe functions
235 An async-cancel-safe function is one that can be safely called
236 in an application where asynchronous cancelability is enabled (see
237 .BR pthread_setcancelstate (3)).
239 Only the following functions are required to be async-cancel-safe by
240 POSIX.1-2001 and POSIX.1-2008:
245 pthread_setcancelstate()
246 pthread_setcanceltype()
249 .SS Cancellation points
250 POSIX.1 specifies that certain functions must,
251 and certain other functions may, be cancellation points.
252 If a thread is cancelable, its cancelability type is deferred,
253 and a cancellation request is pending for the thread,
254 then the thread is canceled when it calls a function
255 that is a cancellation point.
257 The following functions are required to be cancellation points by
258 POSIX.1-2001 and/or POSIX.1-2008:
261 .\" Document the list of all functions that are cancellation points in glibc
285 openat() [Added in POSIX.1-2008]
290 pthread_cond_timedwait()
308 sigpause() [POSIX.1-2001 only (moves to "may" list in POSIX.1-2008)]
316 usleep() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
325 The following functions may be cancellation points according to
326 POSIX.1-2001 and/or POSIX.1-2008:
336 chmod() [Added in POSIX.1-2008]
337 chown() [Added in POSIX.1-2008]
351 dprintf() [Added in POSIX.1-2008]
359 faccessat() [Added in POSIX.1-2008]
360 fchmod() [Added in POSIX.1-2008]
361 fchmodat() [Added in POSIX.1-2008]
362 fchown() [Added in POSIX.1-2008]
363 fchownat() [Added in POSIX.1-2008]
365 fcntl() (for any value of cmd argument)
387 fstatat() [Added in POSIX.1-2008]
391 futimens() [Added in POSIX.1-2008]
402 getdelim() [Added in POSIX.1-2008]
408 gethostbyaddr() [SUSv3 only (function removed in POSIX.1-2008)]
409 gethostbyname() [SUSv3 only (function removed in POSIX.1-2008)]
413 getline() [Added in POSIX.1-2008]
420 getopt() (if opterr is nonzero)
438 getwd() [SUSv3 only (function removed in POSIX.1-2008)]
444 linkat() [Added in POSIX.1-2008]
445 lio_listio() [Added in POSIX.1-2008]
448 lockf() [Added in POSIX.1-2008]
451 mkdir() [Added in POSIX.1-2008]
452 mkdirat() [Added in POSIX.1-2008]
453 mkdtemp() [Added in POSIX.1-2008]
454 mkfifo() [Added in POSIX.1-2008]
455 mkfifoat() [Added in POSIX.1-2008]
456 mknod() [Added in POSIX.1-2008]
457 mknodat() [Added in POSIX.1-2008]
476 posix_trace_create_withlog()
477 posix_trace_eventtypelist_getnext_id()
478 posix_trace_eventtypelist_rewind()
480 posix_trace_get_attr()
481 posix_trace_get_filter()
482 posix_trace_get_status()
483 posix_trace_getnext_event()
486 posix_trace_set_filter()
487 posix_trace_shutdown()
488 posix_trace_timedgetnext_event()
489 posix_typed_mem_open()
491 psiginfo() [Added in POSIX.1-2008]
492 psignal() [Added in POSIX.1-2008]
493 pthread_rwlock_rdlock()
494 pthread_rwlock_timedrdlock()
495 pthread_rwlock_timedwrlock()
496 pthread_rwlock_wrlock()
507 readlink() [Added in POSIX.1-2008]
508 readlinkat() [Added in POSIX.1-2008]
511 renameat() [Added in POSIX.1-2008]
514 scandir() [Added in POSIX.1-2008]
525 sigpause() [Added in POSIX.1-2008]
531 symlinkat() [Added in POSIX.1-2008]
542 unlinkat() [Added in POSIX.1-2008]
543 utime() [Added in POSIX.1-2008]
544 utimensat() [Added in POSIX.1-2008]
545 utimes() [Added in POSIX.1-2008]
546 vdprintf() [Added in POSIX.1-2008]
558 An implementation may also mark other functions
559 not specified in the standard as cancellation points.
560 In particular, an implementation is likely to mark
561 any nonstandard function that may block as a cancellation point.
562 (This includes most functions that can touch files.)
563 .\" So, scanning "cancellation point" comments in the glibc 2.8 header
564 .\" files, it looks as though at least the following nonstandard
565 .\" functions are cancellation points:
605 .\" getprotobynumber_r
620 .\" getwchar_unlocked
627 .\" pthread_timedjoin_np
633 .\" putwchar_unlocked
653 .SS Compiling on Linux
654 On Linux, programs that use the Pthreads API should be compiled using
656 .SS Linux implementations of POSIX threads
657 Over time, two threading implementations have been provided by
658 the GNU C library on Linux:
661 This is the original Pthreads implementation.
662 Since glibc 2.4, this implementation is no longer supported.
664 .BR NPTL " (Native POSIX Threads Library)"
665 This is the modern Pthreads implementation.
666 By comparison with LinuxThreads, NPTL provides closer conformance to
667 the requirements of the POSIX.1 specification and better performance
668 when creating large numbers of threads.
669 NPTL is available since glibc 2.3.2,
670 and requires features that are present in the Linux 2.6 kernel.
672 Both of these are so-called 1:1 implementations, meaning that each
673 thread maps to a kernel scheduling entity.
674 Both threading implementations employ the Linux
677 In NPTL, thread synchronization primitives (mutexes,
678 thread joining, and so on) are implemented using the Linux
682 The notable features of this implementation are the following:
684 In addition to the main (initial) thread,
685 and the threads that the program creates using
686 .BR pthread_create (3),
687 the implementation creates a "manager" thread.
688 This thread handles thread creation and termination.
689 (Problems can result if this thread is inadvertently killed.)
691 Signals are used internally by the implementation.
692 On Linux 2.2 and later, the first three real-time signals are used
695 On older Linux kernels,
700 Applications must avoid the use of whichever set of signals is
701 employed by the implementation.
703 Threads do not share process IDs.
704 (In effect, LinuxThreads threads are implemented as processes which share
705 more information than usual, but which do not share a common process ID.)
706 LinuxThreads threads (including the manager thread)
707 are visible as separate processes using
710 The LinuxThreads implementation deviates from the POSIX.1
711 specification in a number of ways, including the following:
715 return a different value in each thread.
719 in threads other than the main thread return the process ID of the
720 manager thread; instead
722 in these threads should return the same value as
726 When one thread creates a new child process using
728 any thread should be able to
731 However, the implementation allows only the thread that
738 all other threads are terminated (as required by POSIX.1).
739 However, the resulting process has the same PID as the thread that called
741 it should have the same PID as the main thread.
743 Threads do not share user and group IDs.
744 This can cause complications with set-user-ID programs and
745 can cause failures in Pthreads functions if an application
746 changes its credentials using
750 Threads do not share a common session ID and process group ID.
752 Threads do not share record locks created using
755 The information returned by
759 is per-thread rather than process-wide.
761 Threads do not share semaphore undo values (see
764 Threads do not share interval timers.
766 Threads do not share a common nice value.
768 POSIX.1 distinguishes the notions of signals that are directed
769 to the process as a whole and signals that are directed to individual
771 According to POSIX.1, a process-directed signal (sent using
773 for example) should be handled by a single,
774 arbitrarily selected thread within the process.
775 LinuxThreads does not support the notion of process-directed signals:
776 signals may be sent only to specific threads.
778 Threads have distinct alternate signal stack settings.
779 However, a new thread's alternate signal stack settings
780 are copied from the thread that created it, so that
781 the threads initially share an alternate signal stack.
782 (A new thread should start with no alternate signal stack defined.
783 If two threads handle signals on their shared alternate signal
784 stack at the same time, unpredictable program failures are
787 With NPTL, all of the threads in a process are placed
788 in the same thread group;
789 all members of a thread group share the same PID.
790 NPTL does not employ a manager thread.
791 NPTL makes internal use of the first two real-time signals
794 these signals cannot be used in applications.
796 NPTL still has at least one nonconformance with POSIX.1:
798 Threads do not share a common nice value.
799 .\" FIXME . bug report filed for NPTL nice nonconformance
800 .\" http://bugzilla.kernel.org/show_bug.cgi?id=6258
801 .\" Sep 08: there is a patch by Denys Vlasenko to address this
802 .\" "make setpriority POSIX compliant; introduce PRIO_THREAD extension"
803 .\" Monitor this to see if it makes it into mainline.
805 Some NPTL nonconformances occur only with older kernels:
807 The information returned by
811 is per-thread rather than process-wide (fixed in kernel 2.6.9).
813 Threads do not share resource limits (fixed in kernel 2.6.10).
815 Threads do not share interval timers (fixed in kernel 2.6.12).
817 Only the main thread is permitted to start a new session using
819 (fixed in kernel 2.6.16).
821 Only the main thread is permitted to make the process into a
822 process group leader using
824 (fixed in kernel 2.6.16).
826 Threads have distinct alternate signal stack settings.
827 However, a new thread's alternate signal stack settings
828 are copied from the thread that created it, so that
829 the threads initially share an alternate signal stack
830 (fixed in kernel 2.6.16).
832 Note the following further points about the NPTL implementation:
834 If the stack size soft resource limit (see the description of
838 is set to a value other than
840 then this value defines the default stack size for new threads.
841 To be effective, this limit must be set before the program
842 is executed, perhaps using the
844 shell built-in command
845 .RI ( "limit stacksize"
847 .SS Determining the threading implementation
848 Since glibc 2.3.2, the
850 command can be used to determine
851 the system's threading implementation, for example:
855 bash$ getconf GNU_LIBPTHREAD_VERSION
860 With older glibc versions, a command such as the following should
861 be sufficient to determine the default threading implementation:
865 bash$ $( ldd /bin/ls | grep libc.so | awk \(aq{print $3}\(aq ) | \\
866 egrep \-i \(aqthreads|nptl\(aq
867 Native POSIX Threads Library by Ulrich Drepper et al
870 .SS Selecting the threading implementation: LD_ASSUME_KERNEL
871 On systems with a glibc that supports both LinuxThreads and NPTL
872 (i.e., glibc 2.3.\fIx\fP), the
874 environment variable can be used to override
875 the dynamic linker's default choice of threading implementation.
876 This variable tells the dynamic linker to assume that it is
877 running on top of a particular kernel version.
878 By specifying a kernel version that does not
879 provide the support required by NPTL, we can force the use
881 (The most likely reason for doing this is to run a
882 (broken) application that depends on some nonconformant behavior
888 bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \\
889 awk \(aq{print $3}\(aq ) | egrep \-i \(aqthreads|nptl\(aq
890 linuxthreads-0.10 by Xavier Leroy
904 Various Pthreads manual pages, for example:
905 .BR pthread_attr_init (3),
906 .BR pthread_atfork (3),
907 .BR pthread_cancel (3),
908 .BR pthread_cleanup_push (3),
909 .BR pthread_cond_signal (3),
910 .BR pthread_cond_wait (3),
911 .BR pthread_create (3),
912 .BR pthread_detach (3),
913 .BR pthread_equal (3),
914 .BR pthread_exit (3),
915 .BR pthread_key_create (3),
916 .BR pthread_kill (3),
917 .BR pthread_mutex_lock (3),
918 .BR pthread_mutex_unlock (3),
919 .BR pthread_once (3),
920 .BR pthread_setcancelstate (3),
921 .BR pthread_setcanceltype (3),
922 .BR pthread_setspecific (3),
923 .BR pthread_sigmask (3),
924 .BR pthread_sigqueue (3),
926 .BR pthread_testcancel (3)
928 This page is part of release 3.79 of the Linux
931 A description of the project,
932 information about reporting bugs,
933 and the latest version of this page,
935 \%http://www.kernel.org/doc/man\-pages/.