1PTHREAD_CREATE(3) Linux Programmer's Manual PTHREAD_CREATE(3)
2
3
4
6 pthread_create - create a new thread
7
9 #include <pthread.h>
10
11 int pthread_create(pthread_t *restrict thread,
12 const pthread_attr_t *restrict attr,
13 void *(*start_routine)(void *),
14 void *restrict arg);
15
16 Compile and link with -pthread.
17
19 The pthread_create() function starts a new thread in the calling
20 process. The new thread starts execution by invoking start_routine();
21 arg is passed as the sole argument of start_routine().
22
23 The new thread terminates in one of the following ways:
24
25 * It calls pthread_exit(3), specifying an exit status value that is
26 available to another thread in the same process that calls
27 pthread_join(3).
28
29 * It returns from start_routine(). This is equivalent to calling
30 pthread_exit(3) with the value supplied in the return statement.
31
32 * It is canceled (see pthread_cancel(3)).
33
34 * Any of the threads in the process calls exit(3), or the main thread
35 performs a return from main(). This causes the termination of all
36 threads in the process.
37
38 The attr argument points to a pthread_attr_t structure whose contents
39 are used at thread creation time to determine attributes for the new
40 thread; this structure is initialized using pthread_attr_init(3) and
41 related functions. If attr is NULL, then the thread is created with
42 default attributes.
43
44 Before returning, a successful call to pthread_create() stores the ID
45 of the new thread in the buffer pointed to by thread; this identifier
46 is used to refer to the thread in subsequent calls to other pthreads
47 functions.
48
49 The new thread inherits a copy of the creating thread's signal mask
50 (pthread_sigmask(3)). The set of pending signals for the new thread is
51 empty (sigpending(2)). The new thread does not inherit the creating
52 thread's alternate signal stack (sigaltstack(2)).
53
54 The new thread inherits the calling thread's floating-point environment
55 (fenv(3)).
56
57 The initial value of the new thread's CPU-time clock is 0 (see
58 pthread_getcpuclockid(3)).
59
60 Linux-specific details
61 The new thread inherits copies of the calling thread's capability sets
62 (see capabilities(7)) and CPU affinity mask (see sched_setaffinity(2)).
63
65 On success, pthread_create() returns 0; on error, it returns an error
66 number, and the contents of *thread are undefined.
67
69 EAGAIN Insufficient resources to create another thread.
70
71 EAGAIN A system-imposed limit on the number of threads was encountered.
72 There are a number of limits that may trigger this error: the
73 RLIMIT_NPROC soft resource limit (set via setrlimit(2)), which
74 limits the number of processes and threads for a real user ID,
75 was reached; the kernel's system-wide limit on the number of
76 processes and threads, /proc/sys/kernel/threads-max, was reached
77 (see proc(5)); or the maximum number of PIDs, /proc/sys/ker‐
78 nel/pid_max, was reached (see proc(5)).
79
80 EINVAL Invalid settings in attr.
81
82 EPERM No permission to set the scheduling policy and parameters speci‐
83 fied in attr.
84
86 For an explanation of the terms used in this section, see at‐
87 tributes(7).
88
89 ┌────────────────────────────────────────────┬───────────────┬─────────┐
90 │Interface │ Attribute │ Value │
91 ├────────────────────────────────────────────┼───────────────┼─────────┤
92 │pthread_create() │ Thread safety │ MT-Safe │
93 └────────────────────────────────────────────┴───────────────┴─────────┘
94
96 POSIX.1-2001, POSIX.1-2008.
97
99 See pthread_self(3) for further information on the thread ID returned
100 in *thread by pthread_create(). Unless real-time scheduling policies
101 are being employed, after a call to pthread_create(), it is indetermi‐
102 nate which thread—the caller or the new thread—will next execute.
103
104 A thread may either be joinable or detached. If a thread is joinable,
105 then another thread can call pthread_join(3) to wait for the thread to
106 terminate and fetch its exit status. Only when a terminated joinable
107 thread has been joined are the last of its resources released back to
108 the system. When a detached thread terminates, its resources are auto‐
109 matically released back to the system: it is not possible to join with
110 the thread in order to obtain its exit status. Making a thread de‐
111 tached is useful for some types of daemon threads whose exit status the
112 application does not need to care about. By default, a new thread is
113 created in a joinable state, unless attr was set to create the thread
114 in a detached state (using pthread_attr_setdetachstate(3)).
115
116 Under the NPTL threading implementation, if the RLIMIT_STACK soft re‐
117 source limit at the time the