1 .\" Copyright (c) 2005 by Michael Kerrisk <mtk.manpages@gmail.com>
3 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
5 .TH pthreads 7 (date) "Linux man-pages (unreleased)"
7 pthreads \- POSIX threads
9 POSIX.1 specifies a set of interfaces (functions, header files) for
10 threaded programming commonly known as POSIX threads, or Pthreads.
11 A single process can contain multiple threads,
12 all of which are executing the same program.
13 These threads share the same global memory (data and heap segments),
14 but each thread has its own stack (automatic variables).
16 POSIX.1 also requires that threads share a range of other attributes
17 (i.e., these attributes are process-wide rather than per-thread):
23 process group ID and session ID
36 file mode creation mask
48 .RB ( timer_create (2))
51 .RB ( setpriority (2))
56 measurements of the consumption of CPU time
61 As well as the stack, POSIX.1 specifies that various other
62 attributes are distinct for each thread, including:
69 .RB ( pthread_sigmask (3))
75 alternate signal stack
76 .RB ( sigaltstack (2))
78 real-time scheduling policy and priority
81 The following Linux-specific features are also per-thread:
87 .RB ( sched_setaffinity (2))
88 .SS Pthreads function return values
89 Most pthreads functions return 0 on success, and an error number on failure.
90 The error numbers that can be returned have the same meaning as
91 the error numbers returned in
93 by conventional system calls and C library functions.
94 Note that the pthreads functions do not set
96 For each of the pthreads functions that can return an error,
97 POSIX.1-2001 specifies that the function can never fail with the error
100 Each of the threads in a process has a unique thread identifier
103 This identifier is returned to the caller of
104 .BR pthread_create (3),
105 and a thread can obtain its own thread identifier using
106 .BR pthread_self (3).
108 Thread IDs are guaranteed to be unique only within a process.
109 (In all pthreads functions that accept a thread ID as an argument,
110 that ID by definition refers to a thread in
111 the same process as the caller.)
113 The system may reuse a thread ID after a terminated thread has been joined,
114 or a detached thread has terminated.
115 POSIX says: "If an application attempts to use a thread ID whose
116 lifetime has ended, the behavior is undefined."
117 .SS Thread-safe functions
118 A thread-safe function is one that can be safely
119 (i.e., it will deliver the same results regardless of whether it is)
120 called from multiple threads at the same time.
122 POSIX.1-2001 and POSIX.1-2008 require that all functions specified
123 in the standard shall be thread-safe,
124 except for the following functions:
132 ctermid() if passed a non-NULL argument
146 ecvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
151 fcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
153 gcvt() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
161 gethostbyaddr() [POSIX.1-2001 only (function removed in
163 gethostbyname() [POSIX.1-2001 only (function removed in
211 strsignal() [Added in POSIX.1-2008]
213 system() [Added in POSIX.1-2008]
214 tmpnam() if passed a non-NULL argument
217 wcrtomb() if its final argument is NULL
218 wcsrtombs() if its final argument is NULL
223 .SS Async-cancel-safe functions
224 An async-cancel-safe function is one that can be safely called
225 in an application where asynchronous cancelability is enabled (see
226 .BR pthread_setcancelstate (3)).
228 Only the following functions are required to be async-cancel-safe by
229 POSIX.1-2001 and POSIX.1-2008:
234 pthread_setcancelstate()
235 pthread_setcanceltype()
238 .SS Cancelation points
239 POSIX.1 specifies that certain functions must,
240 and certain other functions may, be cancelation points.
241 If a thread is cancelable, its cancelability type is deferred,
242 and a cancelation request is pending for the thread,
243 then the thread is canceled when it calls a function
244 that is a cancelation point.
246 The following functions are required to be cancelation points by
247 POSIX.1-2001 and/or POSIX.1-2008:
250 .\" Document the list of all functions that are cancelation points in glibc
274 openat() [Added in POSIX.1-2008]
279 pthread_cond_timedwait()
297 sigpause() [POSIX.1-2001 only (moves to "may" list in POSIX.1-2008)]
305 usleep() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
314 The following functions may be cancelation points according to
315 POSIX.1-2001 and/or POSIX.1-2008:
325 chmod() [Added in POSIX.1-2008]
326 chown() [Added in POSIX.1-2008]
340 dprintf() [Added in POSIX.1-2008]
348 faccessat() [Added in POSIX.1-2008]
349 fchmod() [Added in POSIX.1-2008]
350 fchmodat() [Added in POSIX.1-2008]
351 fchown() [Added in POSIX.1-2008]
352 fchownat() [Added in POSIX.1-2008]
354 fcntl() (for any value of cmd argument)
376 fstatat() [Added in POSIX.1-2008]
380 futimens() [Added in POSIX.1-2008]
391 getdelim() [Added in POSIX.1-2008]
397 gethostbyaddr() [POSIX.1-2001 only (function removed in
399 gethostbyname() [POSIX.1-2001 only (function removed in
404 getline() [Added in POSIX.1-2008]
411 getopt() (if opterr is nonzero)
429 getwd() [POSIX.1-2001 only (function removed in POSIX.1-2008)]
435 linkat() [Added in POSIX.1-2008]
436 lio_listio() [Added in POSIX.1-2008]
439 lockf() [Added in POSIX.1-2008]
442 mkdir() [Added in POSIX.1-2008]
443 mkdirat() [Added in POSIX.1-2008]
444 mkdtemp() [Added in POSIX.1-2008]
445 mkfifo() [Added in POSIX.1-2008]
446 mkfifoat() [Added in POSIX.1-2008]
447 mknod() [Added in POSIX.1-2008]
448 mknodat() [Added in POSIX.1-2008]
467 posix_trace_create_withlog()
468 posix_trace_eventtypelist_getnext_id()
469 posix_trace_eventtypelist_rewind()
471 posix_trace_get_attr()
472 posix_trace_get_filter()
473 posix_trace_get_status()
474 posix_trace_getnext_event()
477 posix_trace_set_filter()
478 posix_trace_shutdown()
479 posix_trace_timedgetnext_event()
480 posix_typed_mem_open()
482 psiginfo() [Added in POSIX.1-2008]
483 psignal() [Added in POSIX.1-2008]
484 pthread_rwlock_rdlock()
485 pthread_rwlock_timedrdlock()
486 pthread_rwlock_timedwrlock()
487 pthread_rwlock_wrlock()
498 readlink() [Added in POSIX.1-2008]
499 readlinkat() [Added in POSIX.1-2008]
502 renameat() [Added in POSIX.1-2008]
505 scandir() [Added in POSIX.1-2008]
516 sigpause() [Added in POSIX.1-2008]
522 symlinkat() [Added in POSIX.1-2008]
533 unlinkat() [Added in POSIX.1-2008]
534 utime() [Added in POSIX.1-2008]
535 utimensat() [Added in POSIX.1-2008]
536 utimes() [Added in POSIX.1-2008]
537 vdprintf() [Added in POSIX.1-2008]
549 An implementation may also mark other functions
550 not specified in the standard as cancelation points.
551 In particular, an implementation is likely to mark
552 any nonstandard function that may block as a cancelation point.
553 (This includes most functions that can touch files.)
555 It should be noted that even if an application is not using
556 asynchronous cancelation, that calling a function from the above list
557 from an asynchronous signal handler may cause the equivalent of
558 asynchronous cancelation.
559 The underlying user code may not expect
560 asynchronous cancelation and the state of the user data may become
562 Therefore signals should be used with caution when
563 entering a region of deferred cancelation.
564 .\" So, scanning "cancelation point" comments in the glibc 2.8 header
565 .\" files, it looks as though at least the following nonstandard
566 .\" functions are cancelation points:
606 .\" getprotobynumber_r
621 .\" getwchar_unlocked
628 .\" pthread_timedjoin_np
634 .\" putwchar_unlocked
654 .SS Compiling on Linux
655 On Linux, programs that use the Pthreads API should be compiled using
657 .SS Linux implementations of POSIX threads
658 Over time, two threading implementations have been provided by
659 the GNU C library on Linux:
662 This is the original Pthreads implementation.
663 Since glibc 2.4, this implementation is no longer supported.
665 .BR NPTL " (Native POSIX Threads Library)"
666 This is the modern Pthreads implementation.
667 By comparison with LinuxThreads, NPTL provides closer conformance to
668 the requirements of the POSIX.1 specification and better performance
669 when creating large numbers of threads.
670 NPTL is available since glibc 2.3.2,
671 and requires features that are present in the Linux 2.6 kernel.
673 Both of these are so-called 1:1 implementations, meaning that each
674 thread maps to a kernel scheduling entity.
675 Both threading implementations employ the Linux
678 In NPTL, thread synchronization primitives (mutexes,
679 thread joining, and so on) are implemented using the Linux
683 The notable features of this implementation are the following:
685 In addition to the main (initial) thread,
686 and the threads that the program creates using
687 .BR pthread_create (3),
688 the implementation creates a "manager" thread.
689 This thread handles thread creation and termination.
690 (Problems can result if this thread is inadvertently killed.)
692 Signals are used internally by the implementation.
693 On Linux 2.2 and later, the first three real-time signals are used
696 On older Linux kernels,
701 Applications must avoid the use of whichever set of signals is
702 employed by the implementation.
704 Threads do not share process IDs.
705 (In effect, LinuxThreads threads are implemented as processes which share
706 more information than usual, but which do not share a common process ID.)
707 LinuxThreads threads (including the manager thread)
708 are visible as separate processes using
711 The LinuxThreads implementation deviates from the POSIX.1
712 specification in a number of ways, including the following:
716 return a different value in each thread.
720 in threads other than the main thread return the process ID of the
721 manager thread; instead
723 in these threads should return the same value as
727 When one thread creates a new child process using
729 any thread should be able to
732 However, the implementation allows only the thread that
739 all other threads are terminated (as required by POSIX.1).
740 However, the resulting process has the same PID as the thread that called
742 it should have the same PID as the main thread.
744 Threads do not share user and group IDs.
745 This can cause complications with set-user-ID programs and
746 can cause failures in Pthreads functions if an application
747 changes its credentials using
751 Threads do not share a common session ID and process group ID.
753 Threads do not share record locks created using
756 The information returned by
760 is per-thread rather than process-wide.
762 Threads do not share semaphore undo values (see
765 Threads do not share interval timers.
767 Threads do not share a common nice value.
769 POSIX.1 distinguishes the notions of signals that are directed
770 to the process as a whole and signals that are directed to individual
772 According to POSIX.1, a process-directed signal (sent using
774 for example) should be handled by a single,
775 arbitrarily selected thread within the process.
776 LinuxThreads does not support the notion of process-directed signals:
777 signals may be sent only to specific threads.
779 Threads have distinct alternate signal stack settings.
780 However, a new thread's alternate signal stack settings
781 are copied from the thread that created it, so that
782 the threads initially share an alternate signal stack.
783 (A new thread should start with no alternate signal stack defined.
784 If two threads handle signals on their shared alternate signal
785 stack at the same time, unpredictable program failures are
788 With NPTL, all of the threads in a process are placed
789 in the same thread group;
790 all members of a thread group share the same PID.
791 NPTL does not employ a manager thread.
793 NPTL makes internal use of the first two real-time signals;
794 these signals cannot be used in applications.
799 NPTL still has at least one nonconformance with POSIX.1:
801 Threads do not share a common nice value.
802 .\" FIXME . bug report filed for NPTL nice nonconformance
803 .\" http://bugzilla.kernel.org/show_bug.cgi?id=6258
804 .\" Sep 08: there is a patch by Denys Vlasenko to address this
805 .\" "make setpriority POSIX compliant; introduce PRIO_THREAD extension"
806 .\" Monitor this to see if it makes it into mainline.
808 Some NPTL nonconformances occur only with older kernels:
810 The information returned by
814 is per-thread rather than process-wide (fixed in Linux 2.6.9).
816 Threads do not share resource limits (fixed in Linux 2.6.10).
818 Threads do not share interval timers (fixed in Linux 2.6.12).
820 Only the main thread is permitted to start a new session using
822 (fixed in Linux 2.6.16).
824 Only the main thread is permitted to make the process into a
825 process group leader using
827 (fixed in Linux 2.6.16).
829 Threads have distinct alternate signal stack settings.
830 However, a new thread's alternate signal stack settings
831 are copied from the thread that created it, so that
832 the threads initially share an alternate signal stack
833 (fixed in Linux 2.6.16).
835 Note the following further points about the NPTL implementation:
837 If the stack size soft resource limit (see the description of
841 is set to a value other than
843 then this value defines the default stack size for new threads.
844 To be effective, this limit must be set before the program
845 is executed, perhaps using the
847 shell built-in command
848 .RI ( "limit stacksize"
850 .SS Determining the threading implementation
851 Since glibc 2.3.2, the
853 command can be used to determine
854 the system's threading implementation, for example:
858 bash$ getconf GNU_LIBPTHREAD_VERSION
863 With older glibc versions, a command such as the following should
864 be sufficient to determine the default threading implementation:
868 bash$ $( ldd /bin/ls | grep libc.so | awk \[aq]{print $3}\[aq] ) | \e
869 egrep \-i \[aq]threads|nptl\[aq]
870 Native POSIX Threads Library by Ulrich Drepper et al
873 .SS Selecting the threading implementation: LD_ASSUME_KERNEL
874 On systems with a glibc that supports both LinuxThreads and NPTL
875 (i.e., glibc 2.3.\fIx\fP), the
877 environment variable can be used to override
878 the dynamic linker's default choice of threading implementation.
879 This variable tells the dynamic linker to assume that it is
880 running on top of a particular kernel version.
881 By specifying a kernel version that does not
882 provide the support required by NPTL, we can force the use
884 (The most likely reason for doing this is to run a
885 (broken) application that depends on some nonconformant behavior
891 bash$ $( LD_ASSUME_KERNEL=2.2.5 ldd /bin/ls | grep libc.so | \e
892 awk \[aq]{print $3}\[aq] ) | egrep \-i \[aq]threads|nptl\[aq]
893 linuxthreads\-0.10 by Xavier Leroy
907 .BR sigevent (3type),
910 Various Pthreads manual pages, for example:
911 .BR pthread_atfork (3),
912 .BR pthread_attr_init (3),
913 .BR pthread_cancel (3),
914 .BR pthread_cleanup_push (3),
915 .BR pthread_cond_signal (3),
916 .BR pthread_cond_wait (3),
917 .BR pthread_create (3),
918 .BR pthread_detach (3),
919 .BR pthread_equal (3),
920 .BR pthread_exit (3),
921 .BR pthread_key_create (3),
922 .BR pthread_kill (3),
923 .BR pthread_mutex_lock (3),
924 .BR pthread_mutex_unlock (3),
925 .BR pthread_mutexattr_destroy (3),
926 .BR pthread_mutexattr_init (3),
927 .BR pthread_once (3),
928 .BR pthread_spin_init (3),
929 .BR pthread_spin_lock (3),
930 .BR pthread_rwlockattr_setkind_np (3),
931 .BR pthread_setcancelstate (3),
932 .BR pthread_setcanceltype (3),
933 .BR pthread_setspecific (3),
934 .BR pthread_sigmask (3),
935 .BR pthread_sigqueue (3),
937 .BR pthread_testcancel (3)