pthread_create(3) Library Functions Manual pthread_create(3) NNAAMMEE pthread_create - create a new thread LLIIBBRRAARRYY POSIX threads library (_l_i_b_p_t_h_r_e_a_d, _-_l_p_t_h_r_e_a_d) SSYYNNOOPPSSIISS ##iinncclluuddee <
>
iinntt pptthhrreeaadd__ccrreeaattee((pptthhrreeaadd__tt **rreessttrriicctt _t_h_r_e_a_d,,
ccoonnsstt pptthhrreeaadd__aattttrr__tt **rreessttrriicctt _a_t_t_r,,
vvooiidd **((**_s_t_a_r_t___r_o_u_t_i_n_e))((vvooiidd **)),,
vvooiidd **rreessttrriicctt _a_r_g));;
DDEESSCCRRIIPPTTIIOONN
The pptthhrreeaadd__ccrreeaattee() function starts a new thread in the calling process. The new thread starts execution by invok‐
ing _s_t_a_r_t___r_o_u_t_i_n_e(); _a_r_g is passed as the sole argument of _s_t_a_r_t___r_o_u_t_i_n_e().
The new thread terminates in one of the following ways:
• It calls pptthhrreeaadd__eexxiitt(3), specifying an exit status value that is available to another thread in the same process
that calls pptthhrreeaadd__jjooiinn(3).
• It returns from _s_t_a_r_t___r_o_u_t_i_n_e(). This is equivalent to calling pptthhrreeaadd__eexxiitt(3) with the value supplied in the
_r_e_t_u_r_n statement.
• It is canceled (see pptthhrreeaadd__ccaanncceell(3)).
• Any of the threads in the process calls eexxiitt(3), or the main thread performs a return from _m_a_i_n(). This causes
the termination of all threads in the process.
The _a_t_t_r argument points to a _p_t_h_r_e_a_d___a_t_t_r___t structure whose contents are used at thread creation time to determine
attributes for the new thread; this structure is initialized using pptthhrreeaadd__aattttrr__iinniitt(3) and related functions. If
_a_t_t_r is NULL, then the thread is created with default attributes.
Before returning, a successful call to pptthhrreeaadd__ccrreeaattee() stores the ID of the new thread in the buffer pointed to by
_t_h_r_e_a_d; this identifier is used to refer to the thread in subsequent calls to other pthreads functions.
The new thread inherits a copy of the creating thread's signal mask (pptthhrreeaadd__ssiiggmmaasskk(3)). The set of pending sig‐
nals for the new thread is empty (ssiiggppeennddiinngg(2)). The new thread does not inherit the creating thread's alternate
signal stack (ssiiggaallttssttaacckk(2)).
The new thread inherits the calling thread's floating-point environment (ffeennvv(3)).
The initial value of the new thread's CPU-time clock is 0 (see pptthhrreeaadd__ggeettccppuucclloocckkiidd(3)).
LLiinnuuxx--ssppeecciiffiicc ddeettaaiillss
The new thread inherits copies of the calling thread's capability sets (see ccaappaabbiilliittiieess(7)) and CPU affinity mask
(see sscchheedd__sseettaaffffiinniittyy(2)).
RREETTUURRNN VVAALLUUEE
On success, pptthhrreeaadd__ccrreeaattee() returns 0; on error, it returns an error number, and the contents of _*_t_h_r_e_a_d are unde‐
fined.
EERRRROORRSS
EEAAGGAAIINN Insufficient resources to create another thread.
EEAAGGAAIINN A system-imposed limit on the number of threads was encountered. There are a number of limits that may trig‐
ger this error: the RRLLIIMMIITT__NNPPRROOCC soft resource limit (set via sseettrrlliimmiitt(2)), which limits the number of pro‐
cesses and threads for a real user ID, was reached; the kernel's system-wide limit on the number of processes
and threads, _/_p_r_o_c_/_s_y_s_/_k_e_r_n_e_l_/_t_h_r_e_a_d_s_-_m_a_x, was reached (see pprroocc(5)); or the maximum number of PIDs,
_/_p_r_o_c_/_s_y_s_/_k_e_r_n_e_l_/_p_i_d___m_a_x, was reached (see pprroocc(5)).
EEIINNVVAALL Invalid settings in _a_t_t_r.
EEPPEERRMM No permission to set the scheduling policy and parameters specified in _a_t_t_r.
AATTTTRRIIBBUUTTEESS
For an explanation of the terms used in this section, see aattttrriibbuutteess(7).
┌─────────────────────────────────────────────────────────────────────────────────────────┬───────────────┬─────────┐
│IInntteerrffaaccee │ AAttttrriibbuuttee │ VVaalluuee │
├─────────────────────────────────────────────────────────────────────────────────────────┼───────────────┼─────────┤
│pptthhrreeaadd__ccrreeaattee() │ Thread safety │ MT-Safe │
└─────────────────────────────────────────────────────────────────────────────────────────┴───────────────┴─────────┘
SSTTAANNDDAARRDDSS
POSIX.1-2001, POSIX.1-2008.
NNOOTTEESS
See pptthhrreeaadd__sseellff(3) for further information on the thread ID returned in _*_t_h_r_e_a_d by pptthhrreeaadd__ccrreeaattee(). Unless real-
time scheduling policies are being employed, after a call to pptthhrreeaadd__ccrreeaattee(), it is indeterminate which thread—the
caller or the new thread—will next execute.
A thread may either be _j_o_i_n_a_b_l_e or _d_e_t_a_c_h_e_d. If a thread is joinable, then another thread can call pptthhrreeaadd__jjooiinn(3)
to wait for the thread to terminate and fetch its exit status. Only when a terminated joinable thread has been
joined are the last of its resources released back to the system. When a detached thread terminates, its resources
are automatically released back to the system: it is not possible to join with the thread in order to obtain its ex‐
it status. Making a thread detached is useful for some types of daemon threads whose exit status the application
does not need to care about. By default, a new thread is created in a joinable state, unless _a_t_t_r was set to create
the thread in a detached state (using pptthhrreeaadd__aattttrr__sseettddeettaacchhssttaattee(3)).
Under the NPTL threading implementation, if the RRLLIIMMIITT__SSTTAACCKK soft resource limit _a_t _t_h_e _t_i_m_e _t_h_e _p_r_o_g_r_a_m _s_t_a_r_t_e_d has
any value other than "unlimited", then it determines the default stack size of new threads. Using pptthhrreeaadd__aattttrr__sseett‐‐
ssttaacckkssiizzee(3), the stack size attribute can be explicitly set in the _a_t_t_r argument used to create a thread, in order
to obtain a stack size other than the default. If the RRLLIIMMIITT__SSTTAACCKK resource limit is set to "unlimited", a per-ar‐
chitecture value is used for the stack size. Here is the value for a few architectures:
┌─────────────┬────────────────────┐
│AArrcchhiitteeccttuurree │ DDeeffaauulltt ssttaacckk ssiizzee │
├─────────────┼────────────────────┤
│i386 │ 2 MB │
├─────────────┼────────────────────┤
│IA-64 │ 32 MB │
├─────────────┼────────────────────┤
│PowerPC │ 4 MB │
├─────────────┼────────────────────┤
│S/390 │ 2 MB │
├─────────────┼────────────────────┤
│Sparc-32 │ 2 MB │
├─────────────┼────────────────────┤
│Sparc-64 │ 4 MB │
├─────────────┼────────────────────┤
│x86_64 │ 2 MB │
└─────────────┴────────────────────┘
BBUUGGSS
In the obsolete LinuxThreads implementation, each of the threads in a process has a different process ID. This is
in violation of the POSIX threads specification, and is the source of many other nonconformances to the standard;
see pptthhrreeaaddss(7).
EEXXAAMMPPLLEESS
The program below demonstrates the use of pptthhrreeaadd__ccrreeaattee(), as well as a number of other functions in the pthreads
API.
In the following run, on a system providing the NPTL threading implementation, the stack size defaults to the value
given by the "stack size" resource limit:
$ uulliimmiitt --ss
8192 # The stack size limit is 8 MB (0x800000 bytes)
$ ..//aa..oouutt hhoollaa ssaalluutt sseerrvvuuss
Thread 1: top of stack near 0xb7dd03b8; argv_string=hola
Thread 2: top of stack near 0xb75cf3b8; argv_string=salut
Thread 3: top of stack near 0xb6dce3b8; argv_string=servus
Joined with thread 1; returned value was HOLA
Joined with thread 2; returned value was SALUT
Joined with thread 3; returned value was SERVUS
In the next run, the program explicitly sets a stack size of 1 MB (using pptthhrreeaadd__aattttrr__sseettssttaacckkssiizzee(3)) for the cre‐
ated threads:
$ ..//aa..oouutt --ss 00xx110000000000 hhoollaa ssaalluutt sseerrvvuuss
Thread 1: top of stack near 0xb7d723b8; argv_string=hola
Thread 2: top of stack near 0xb7c713b8; argv_string=salut
Thread 3: top of stack near 0xb7b703b8; argv_string=servus
Joined with thread 1; returned value was HOLA
Joined with thread 2; returned value was SALUT
Joined with thread 3; returned value was SERVUS
PPrrooggrraamm ssoouurrccee
#include