2 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3 .\" <mtk.manpages@gmail.com>
5 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
7 .TH pthread_create 3 (date) "Linux man-pages (unreleased)"
9 pthread_create \- create a new thread
12 .RI ( libpthread ", " \-lpthread )
15 .B #include <pthread.h>
17 .BI "int pthread_create(pthread_t *restrict " thread ,
18 .BI " const pthread_attr_t *restrict " attr ,
19 .BI " void *(*" start_routine ")(void *),"
20 .BI " void *restrict " arg );
25 function starts a new thread in the calling process.
26 The new thread starts execution by invoking
29 is passed as the sole argument of
32 The new thread terminates in one of the following ways:
36 specifying an exit status value that is available to another thread
37 in the same process that calls
42 This is equivalent to calling
44 with the value supplied in the
49 .BR pthread_cancel (3)).
51 Any of the threads in the process calls
53 or the main thread performs a return from
55 This causes the termination of all threads in the process.
61 structure whose contents are used at thread creation time to
62 determine attributes for the new thread;
63 this structure is initialized using
64 .BR pthread_attr_init (3)
65 and related functions.
69 then the thread is created with default attributes.
71 Before returning, a successful call to
73 stores the ID of the new thread in the buffer pointed to by
75 this identifier is used to refer to the thread
76 in subsequent calls to other pthreads functions.
78 The new thread inherits a copy of the creating thread's signal mask
79 .RB ( pthread_sigmask (3)).
80 The set of pending signals for the new thread is empty
81 .RB ( sigpending (2)).
82 The new thread does not inherit the creating thread's
83 alternate signal stack
84 .RB ( sigaltstack (2)).
86 The new thread inherits the calling thread's floating-point environment
89 The initial value of the new thread's CPU-time clock is 0
91 .BR pthread_getcpuclockid (3)).
92 .\" CLOCK_THREAD_CPUTIME_ID in clock_gettime(2)
93 .SS Linux-specific details
94 The new thread inherits copies of the calling thread's capability sets
97 and CPU affinity mask (see
98 .BR sched_setaffinity (2)).
101 .BR pthread_create ()
103 on error, it returns an error number, and the contents of
109 Insufficient resources to create another thread.
112 .\" NOTE! The following should match the description in fork(2)
113 A system-imposed limit on the number of threads was encountered.
114 There are a number of limits that may trigger this error: the
116 soft resource limit (set via
118 which limits the number of processes and threads for a real user ID,
120 the kernel's system-wide limit on the number of processes and threads,
121 .IR /proc/sys/kernel/threads\-max ,
124 or the maximum number of PIDs,
125 .IR /proc/sys/kernel/pid_max ,
133 .\" FIXME . Test the following
135 No permission to set the scheduling policy and parameters specified in
138 For an explanation of the terms used in this section, see
144 Interface Attribute Value
148 .BR pthread_create ()
149 T} Thread safety MT-Safe
158 for further information on the thread ID returned in
161 .BR pthread_create ().
162 Unless real-time scheduling policies are being employed,
164 .BR pthread_create (),
165 it is indeterminate which thread\[em]the caller or the new thread\[em]will
168 A thread may either be
172 If a thread is joinable, then another thread can call
174 to wait for the thread to terminate and fetch its exit status.
175 Only when a terminated joinable thread has been joined are
176 the last of its resources released back to the system.
177 When a detached thread terminates,
178 its resources are automatically released back to the system:
179 it is not possible to join with the thread in order to obtain
181 Making a thread detached is useful for some types of daemon threads
182 whose exit status the application does not need to care about.
183 By default, a new thread is created in a joinable state, unless
185 was set to create the thread in a detached state (using
186 .BR pthread_attr_setdetachstate (3)).
188 Under the NPTL threading implementation, if the
191 .I at the time the program started
192 has any value other than "unlimited",
193 then it determines the default stack size of new threads.
195 .BR pthread_attr_setstacksize (3),
196 the stack size attribute can be explicitly set in the
198 argument used to create a thread,
199 in order to obtain a stack size other than the default.
202 resource limit is set to "unlimited",
203 a per-architecture value is used for the stack size.
204 Here is the value for a few architectures:
210 Architecture Default stack size
221 In the obsolete LinuxThreads implementation,
222 each of the threads in a process has a different process ID.
223 This is in violation of the POSIX threads specification,
224 and is the source of many other nonconformances to the standard; see
227 The program below demonstrates the use of
228 .BR pthread_create (),
229 as well as a number of other functions in the pthreads API.
231 In the following run,
232 on a system providing the NPTL threading implementation,
233 the stack size defaults to the value given by the
234 "stack size" resource limit:
238 .RB "$" " ulimit \-s"
239 8192 # The stack size limit is 8 MB (0x800000 bytes)
240 .RB "$" " ./a.out hola salut servus"
241 Thread 1: top of stack near 0xb7dd03b8; argv_string=hola
242 Thread 2: top of stack near 0xb75cf3b8; argv_string=salut
243 Thread 3: top of stack near 0xb6dce3b8; argv_string=servus
244 Joined with thread 1; returned value was HOLA
245 Joined with thread 2; returned value was SALUT
246 Joined with thread 3; returned value was SERVUS
250 In the next run, the program explicitly sets a stack size of 1\ MB (using
251 .BR pthread_attr_setstacksize (3))
252 for the created threads:
256 .RB "$" " ./a.out \-s 0x100000 hola salut servus"
257 Thread 1: top of stack near 0xb7d723b8; argv_string=hola
258 Thread 2: top of stack near 0xb7c713b8; argv_string=salut
259 Thread 3: top of stack near 0xb7b703b8; argv_string=servus
260 Joined with thread 1; returned value was HOLA
261 Joined with thread 2; returned value was SALUT
262 Joined with thread 3; returned value was SERVUS
267 .\" SRC BEGIN (pthread_create.c)
277 #define handle_error_en(en, msg) \e
278 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
280 #define handle_error(msg) \e
281 do { perror(msg); exit(EXIT_FAILURE); } while (0)
283 struct thread_info { /* Used as argument to thread_start() */
284 pthread_t thread_id; /* ID returned by pthread_create() */
285 int thread_num; /* Application\-defined thread # */
286 char *argv_string; /* From command\-line argument */
289 /* Thread start function: display address near top of our stack,
290 and return upper\-cased copy of argv_string. */
293 thread_start(void *arg)
295 struct thread_info *tinfo = arg;
298 printf("Thread %d: top of stack near %p; argv_string=%s\en",
299 tinfo\->thread_num, (void *) &tinfo, tinfo\->argv_string);
301 uargv = strdup(tinfo\->argv_string);
303 handle_error("strdup");
305 for (char *p = uargv; *p != \[aq]\e0\[aq]; p++)
312 main(int argc, char *argv[])
319 struct thread_info *tinfo;
321 /* The "\-s" option specifies a stack size for our threads. */
324 while ((opt = getopt(argc, argv, "s:")) != \-1) {
327 stack_size = strtoul(optarg, NULL, 0);
331 fprintf(stderr, "Usage: %s [\-s stack\-size] arg...\en",
337 num_threads = argc \- optind;
339 /* Initialize thread creation attributes. */
341 s = pthread_attr_init(&attr);
343 handle_error_en(s, "pthread_attr_init");
345 if (stack_size > 0) {
346 s = pthread_attr_setstacksize(&attr, stack_size);
348 handle_error_en(s, "pthread_attr_setstacksize");
351 /* Allocate memory for pthread_create() arguments. */
353 tinfo = calloc(num_threads, sizeof(*tinfo));
355 handle_error("calloc");
357 /* Create one thread for each command\-line argument. */
359 for (size_t tnum = 0; tnum < num_threads; tnum++) {
360 tinfo[tnum].thread_num = tnum + 1;
361 tinfo[tnum].argv_string = argv[optind + tnum];
363 /* The pthread_create() call stores the thread ID into
364 corresponding element of tinfo[]. */
366 s = pthread_create(&tinfo[tnum].thread_id, &attr,
367 &thread_start, &tinfo[tnum]);
369 handle_error_en(s, "pthread_create");
372 /* Destroy the thread attributes object, since it is no
375 s = pthread_attr_destroy(&attr);
377 handle_error_en(s, "pthread_attr_destroy");
379 /* Now join with each thread, and display its returned value. */
381 for (size_t tnum = 0; tnum < num_threads; tnum++) {
382 s = pthread_join(tinfo[tnum].thread_id, &res);
384 handle_error_en(s, "pthread_join");
386 printf("Joined with thread %d; returned value was %s\en",
387 tinfo[tnum].thread_num, (char *) res);
388 free(res); /* Free memory allocated by thread */
400 .BR pthread_attr_init (3),
401 .BR pthread_cancel (3),
402 .BR pthread_detach (3),
403 .BR pthread_equal (3),
404 .BR pthread_exit (3),
405 .BR pthread_getattr_np (3),
406 .BR pthread_join (3),
407 .BR pthread_self (3),
408 .BR pthread_setattr_default_np (3),