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 2019-03-06 "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 on 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).
125 Thread IDs are guaranteed to be unique only within a process.
126 (In all pthreads functions that accept a thread ID as an argument,
127 that ID by definition refers to a thread in
128 the same process as the caller.)
130 The system may reuse a thread ID after a terminated thread has been joined,
131 or a detached thread has terminated.
132 POSIX says: "If an application attempts to use a thread ID whose
133 lifetime has ended, the behavior is undefined."
134 .SS Thread-safe functions
135 A thread-safe function is one that can be safely
136 (i.e., it will deliver the same results regardless of whether it is)
137 called from multiple threads at the same time.
139 POSIX.1-2001 and POSIX.1-2008 require that all functions specified
140 in the standard shall be thread-safe,
141 except for the following functions:
149 ctermid() if passed a non-NULL argument
163 ecvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
168 fcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
170 gcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
178 gethostbyaddr() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
179 gethostbyname() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
226 strsignal() [Added in POSIX.1-2008]
228 system() [Added in POSIX.1-2008]
229 tmpnam() if passed a non-NULL argument
232 wcrtomb() if its final argument is NULL
233 wcsrtombs() if its final argument is NULL
238 .SS Async-cancel-safe functions
239 An async-cancel-safe function is one that can be safely called
240 in an application where asynchronous cancelability is enabled (see
241 .BR pthread_setcancelstate (3)).
243 Only the following functions are required to be async-cancel-safe by
244 POSIX.1-2001 and POSIX.1-2008:
249 pthread_setcancelstate()
250 pthread_setcanceltype()
253 .SS Cancellation points
254 POSIX.1 specifies that certain functions must,
255 and certain other functions may, be cancellation points.
256 If a thread is cancelable, its cancelability type is deferred,
257 and a cancellation request is pending for the thread,
258 then the thread is canceled when it calls a function
259 that is a cancellation point.
261 The following functions are required to be cancellation points by
262 POSIX.1-2001 and/or POSIX.1-2008:
265 .\" Document the list of all functions that are cancellation points in glibc
289 openat() [Added in POSIX.1-2008]
294 pthread_cond_timedwait()
312 sigpause() [POSIX.1-2001 only (moves to "may" list in POSIX.1-2008)]
320 usleep() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
329 The following functions may be cancellation points according to
330 POSIX.1-2001 and/or POSIX.1-2008:
340 chmod() [Added in POSIX.1-2008]
341 chown() [Added in POSIX.1-2008]
355 dprintf() [Added in POSIX.1-2008]
363 faccessat() [Added in POSIX.1-2008]
364 fchmod() [Added in POSIX.1-2008]
365 fchmodat() [Added in POSIX.1-2008]
366 fchown() [Added in POSIX.1-2008]
367 fchownat() [Added in POSIX.1-2008]
369 fcntl() (for any value of cmd argument)
391 fstatat() [Added in POSIX.1-2008]
395 futimens() [Added in POSIX.1-2008]
406 getdelim() [Added in POSIX.1-2008]
412 gethostbyaddr() [SUSv3 only (function removed in POSIX.1-2008)]
413 gethostbyname() [SUSv3 only (function removed in POSIX.1-2008)]
417 getline() [Added in POSIX.1-2008]
424 getopt() (if opterr is nonzero)
442 getwd() [SUSv3 only (function removed in POSIX.1-2008)]
448 linkat() [Added in POSIX.1-2008]
449 lio_listio() [Added in POSIX.1-2008]
452 lockf() [Added in POSIX.1-2008]
455 mkdir() [Added in POSIX.1-2008]
456 mkdirat() [Added in POSIX.1-2008]
457 mkdtemp() [Added in POSIX.1-2008]
458 mkfifo() [Added in POSIX.1-2008]
459 mkfifoat() [Added in POSIX.1-2008]
460 mknod() [Added in POSIX.1-2008]
461 mknodat() [Added in POSIX.1-2008]
480 posix_trace_create_withlog()
481 posix_trace_eventtypelist_getnext_id()
482 posix_trace_eventtypelist_rewind()
484 posix_trace_get_attr()
485 posix_trace_get_filter()
486 posix_trace_get_status()
487 posix_trace_getnext_event()
490 posix_trace_set_filter()
491 posix_trace_shutdown()
492 posix_trace_timedgetnext_event()
493 posix_typed_mem_open()
495 psiginfo() [Added in POSIX.1-2008]
496 psignal() [Added in POSIX.1-2008]
497 pthread_rwlock_rdlock()
498 pthread_rwlock_timedrdlock()
499 pthread_rwlock_timedwrlock()
500 pthread_rwlock_wrlock()
511 readlink() [Added in POSIX.1-2008]
512 readlinkat() [Added in POSIX.1-2008]
515 renameat() [Added in POSIX.1-2008]
518 scandir() [Added in POSIX.1-2008]
529 sigpause() [Added in POSIX.1-2008]
535 symlinkat() [Added in POSIX.1-2008]
546 unlinkat() [Added in POSIX.1-2008]
547 utime() [Added in POSIX.1-2008]
548 utimensat() [Added in POSIX.1-2008]
549 utimes() [Added in POSIX.1-2008]
550 vdprintf() [Added in POSIX.1-2008]
562 An implementation may also mark other functions
563 not specified in the standard as cancellation points.
564 In particular, an implementation is likely to mark
565 any nonstandard function that may block as a cancellation point.
566 (This includes most functions that can touch files.)
567 .\" So, scanning "cancellation point" comments in the glibc 2.8 header
568 .\" files, it looks as though at least the following nonstandard
569 .\" functions are cancellation points:
609 .\" getprotobynumber_r
624 .\" getwchar_unlocked
631 .\" pthread_timedjoin_np
637 .\" putwchar_unlocked
657 .SS Compiling on Linux
658 On Linux, programs that use the Pthreads API should be compiled using
660 .SS Linux implementations of POSIX threads
661 Over time, two threading implementations have been provided by
662 the GNU C library on Linux:
665 This is the original Pthreads implementation.
666 Since glibc 2.4, this implementation is no longer supported.
668 .BR NPTL " (Native POSIX Threads Library)"
669 This is the modern Pthreads implementation.
670 By comparison with LinuxThreads, NPTL provides closer conformance to
671 the requirements of the POSIX.1 specification and better performance
672 when creating large numbers of threads.
673 NPTL is available since glibc 2.3.2,
674 and requires features that are present in the Linux 2.6 kernel.
676 Both of these are so-called 1:1 implementations, meaning that each
677 thread maps to a kernel scheduling entity.
678 Both threading implementations employ the Linux
681 In NPTL, thread synchronization primitives (mutexes,
682 thread joining, and so on) are implemented using the Linux
686 The notable features of this implementation are the following:
688 In addition to the main (initial) thread,
689 and the threads that the program creates using
690 .BR pthread_create (3),
691 the implementation creates a "manager" thread.
692 This thread handles thread creation and termination.
693 (Problems can result if this thread is inadvertently killed.)
695 Signals are used internally by the implementation.
696 On Linux 2.2 and later, the first three real-time signals are used
699 On older Linux kernels,
704 Applications must avoid the use of whichever set of signals is
705 employed by the implementation.
707 Threads do not share process IDs.
708 (In effect, LinuxThreads threads are implemented as processes which share
709 more information than usual, but which do not share a common process ID.)
710 LinuxThreads threads (including the manager thread)
711 are visible as separate processes using
714 The LinuxThreads implementation deviates from the POSIX.1
715 specification in a number of ways, including the following:
719 return a different value in each thread.
723 in threads other than the main thread return the process ID of the
724 manager thread; instead
726 in these threads should return the same value as
730 When one thread creates a new child process using
732 any thread should be able to
735 However, the implementation allows only the thread that
742 all other threads are terminated (as required by POSIX.1).
743 However, the resulting process has the same PID as the thread that called
745 it should have the same PID as the main thread.
747 Threads do not share user and group IDs.
748 This can cause complications with set-user-ID programs and
749 can cause failures in Pthreads functions if an application
750 changes its credentials using
754 Threads do not share a common session ID and process group ID.
756 Threads do not share record locks created using
759 The information returned by
763 is per-thread rather than process-wide.
765 Threads do not share semaphore undo values (see
768 Threads do not share interval timers.
770 Threads do not share a common nice value.
772 POSIX.1 distinguishes the notions of signals that are directed
773 to the process as a whole and signals that are directed to individual
775 According to POSIX.1, a process-directed signal (sent using
777 for example) should be handled by a single,
778 arbitrarily selected thread within the process.
779 LinuxThreads does not support the notion of process-directed signals:
780 signals may be sent only to specific threads.
782 Threads have distinct alternate signal stack settings.
783 However, a new thread's alternate signal stack settings
784 are copied from the thread that created it, so that
785 the threads initially share an alternate signal stack.
786 (A new thread should start with no alternate signal stack defined.
787 If two threads handle signals on their shared alternate signal
788 stack at the same time, unpredictable program failures are
791 With NPTL, all of the threads in a process are placed
792 in the same thread group;
793 all members of a thread group share the same PID.
794 NPTL does not employ a manager thread.
796 NPTL makes internal use of the first two real-time signals;
797 these signals cannot be used in applications.
802 NPTL still has at least one nonconformance with POSIX.1:
804 Threads do not share a common nice value.
805 .\" FIXME . bug report filed for NPTL nice nonconformance
806 .\" http://bugzilla.kernel.org/show_bug.cgi?id=6258
807 .\" Sep 08: there is a patch by Denys Vlasenko to address this
808 .\" "make setpriority POSIX compliant; introduce PRIO_THREAD extension"
809 .\" Monitor this to see if it makes it into mainline.
811 Some NPTL nonconformances occur only with older kernels:
813 The information returned by
817 is per-thread rather than process-wide (fixed in kernel 2.6.9).
819 Threads do not share resource limits (fixed in kernel 2.6.10).
821 Threads do not share interval timers (fixed in kernel 2.6.12).
823 Only the main thread is permitted to start a new session using
825 (fixed in kernel 2.6.16).
827 Only the main thread is permitted to make the process into a
828 process group leader using
830 (fixed in kernel 2.6.16).
832 Threads have distinct alternate signal stack settings.
833 However, a new thread's alternate signal stack settings
834 are copied from the thread that created it, so that
835 the threads initially share an alternate signal stack
836 (fixed in kernel 2.6.16).
838 Note the following further points about the NPTL implementation:
840 If the stack size soft resource limit (see the description of
844 is set to a value other than
846 then this value defines the default stack size for new threads.
847 To be effective, this limit must be set before the program
848 is executed, perhaps using the
850 shell built-in command
851 .RI ( "limit stacksize"
853 .SS Determining the threading implementation
854 Since glibc 2.3.2, the
856 command can be used to determine
857 the system's threading implementation, for example:
861 bash$ getconf GNU_LIBPTHREAD_VERSION
866 With older glibc versions, a command such as the following should
867 be sufficient to determine the default threading implementation:
871 bash$ $( ldd /bin/ls | grep libc.so | awk \(aq{print $3}\(aq ) | \e
872 egrep \-i \(aqthreads|nptl\(aq
873 Native POSIX Threads Library by Ulrich Drepper et al
876 .SS Selecting the threading implementation: LD_ASSUME_KERNEL
877 On systems with a glibc that supports both LinuxThreads and NPTL
878 (i.e., glibc 2.3.\fIx\fP), the
880 environment variable can be used to override
881 the dynamic linker's default choice of threading implementation.
882 This variable tells the dynamic linker to assume that it is
883 running on top of a particular kernel version.
884 By specifying a kernel version that does not
885 provide the support required by NPTL, we can force the use
887 (The most likely reason for doing this is to run a
888 (broken) application that depends on some nonconformant behavior
894 bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \e
895 awk \(aq{print $3}\(aq ) | egrep \-i \(aqthreads|nptl\(aq
896 linuxthreads-0.10 by Xavier Leroy
913 Various Pthreads manual pages, for example:
914 .BR pthread_atfork (3),
915 .BR pthread_attr_init (3),
916 .BR pthread_cancel (3),
917 .BR pthread_cleanup_push (3),
918 .BR pthread_cond_signal (3),
919 .BR pthread_cond_wait (3),
920 .BR pthread_create (3),
921 .BR pthread_detach (3),
922 .BR pthread_equal (3),
923 .BR pthread_exit (3),
924 .BR pthread_key_create (3),
925 .BR pthread_kill (3),
926 .BR pthread_mutex_lock (3),
927 .BR pthread_mutex_unlock (3),
928 .BR pthread_mutexattr_destroy (3),
929 .BR pthread_mutexattr_init (3),
930 .BR pthread_once (3),
931 .BR pthread_spin_init (3),
932 .BR pthread_spin_lock (3),
933 .BR pthread_rwlockattr_setkind_np (3),
934 .BR pthread_setcancelstate (3),
935 .BR pthread_setcanceltype (3),
936 .BR pthread_setspecific (3),
937 .BR pthread_sigmask (3),
938 .BR pthread_sigqueue (3),
940 .BR pthread_testcancel (3)