1 .\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
2 .\" <mtk.manpages@gmail.com>
4 .\" SPDX-License-Identifier: Linux-man-pages-copyleft
6 .TH PTHREAD_CREATE 3 2021-03-22 "Linux man-pages (unreleased)"
8 pthread_create \- create a new thread
11 .RI ( libpthread ", " \-lpthread )
14 .B #include <pthread.h>
16 .BI "int pthread_create(pthread_t *restrict " thread ,
17 .BI " const pthread_attr_t *restrict " attr ,
18 .BI " void *(*" start_routine ")(void *),"
19 .BI " void *restrict " arg );
24 function starts a new thread in the calling process.
25 The new thread starts execution by invoking
28 is passed as the sole argument of
31 The new thread terminates in one of the following ways:
35 specifying an exit status value that is available to another thread
36 in the same process that calls
41 This is equivalent to calling
43 with the value supplied in the
48 .BR pthread_cancel (3)).
50 Any of the threads in the process calls
52 or the main thread performs a return from
54 This causes the termination of all threads in the process.
60 structure whose contents are used at thread creation time to
61 determine attributes for the new thread;
62 this structure is initialized using
63 .BR pthread_attr_init (3)
64 and related functions.
68 then the thread is created with default attributes.
70 Before returning, a successful call to
72 stores the ID of the new thread in the buffer pointed to by
74 this identifier is used to refer to the thread
75 in subsequent calls to other pthreads functions.
77 The new thread inherits a copy of the creating thread's signal mask
78 .RB ( pthread_sigmask (3)).
79 The set of pending signals for the new thread is empty
80 .RB ( sigpending (2)).
81 The new thread does not inherit the creating thread's
82 alternate signal stack
83 .RB ( sigaltstack (2)).
85 The new thread inherits the calling thread's floating-point environment
88 The initial value of the new thread's CPU-time clock is 0
90 .BR pthread_getcpuclockid (3)).
91 .\" CLOCK_THREAD_CPUTIME_ID in clock_gettime(2)
92 .SS Linux-specific details
93 The new thread inherits copies of the calling thread's capability sets
96 and CPU affinity mask (see
97 .BR sched_setaffinity (2)).
100 .BR pthread_create ()
102 on error, it returns an error number, and the contents of
108 Insufficient resources to create another thread.
111 .\" NOTE! The following should match the description in fork(2)
112 A system-imposed limit on the number of threads was encountered.
113 There are a number of limits that may trigger this error: the
115 soft resource limit (set via
117 which limits the number of processes and threads for a real user ID,
119 the kernel's system-wide limit on the number of processes and threads,
120 .IR /proc/sys/kernel/threads\-max ,
123 or the maximum number of PIDs,
124 .IR /proc/sys/kernel/pid_max ,
132 .\" FIXME . Test the following
134 No permission to set the scheduling policy and parameters specified in
137 For an explanation of the terms used in this section, see
145 Interface Attribute Value
147 .BR pthread_create ()
148 T} Thread safety MT-Safe
154 POSIX.1-2001, POSIX.1-2008.
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\(emthe caller or the new thread\(emwill
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
276 #define handle_error_en(en, msg) \e
277 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
279 #define handle_error(msg) \e
280 do { perror(msg); exit(EXIT_FAILURE); } while (0)
282 struct thread_info { /* Used as argument to thread_start() */
283 pthread_t thread_id; /* ID returned by pthread_create() */
284 int thread_num; /* Application\-defined thread # */
285 char *argv_string; /* From command\-line argument */
288 /* Thread start function: display address near top of our stack,
289 and return upper\-cased copy of argv_string. */
292 thread_start(void *arg)
294 struct thread_info *tinfo = arg;
297 printf("Thread %d: top of stack near %p; argv_string=%s\en",
298 tinfo\->thread_num, (void *) &tinfo, tinfo\->argv_string);
300 uargv = strdup(tinfo\->argv_string);
302 handle_error("strdup");
304 for (char *p = uargv; *p != \(aq\e0\(aq; p++)
311 main(int argc, char *argv[])
313 int s, opt, num_threads;
318 /* The "\-s" option specifies a stack size for our threads. */
321 while ((opt = getopt(argc, argv, "s:")) != \-1) {
324 stack_size = strtoul(optarg, NULL, 0);
328 fprintf(stderr, "Usage: %s [\-s stack\-size] arg...\en",
334 num_threads = argc \- optind;
336 /* Initialize thread creation attributes. */
338 s = pthread_attr_init(&attr);
340 handle_error_en(s, "pthread_attr_init");
342 if (stack_size > 0) {
343 s = pthread_attr_setstacksize(&attr, stack_size);
345 handle_error_en(s, "pthread_attr_setstacksize");
348 /* Allocate memory for pthread_create() arguments. */
350 struct thread_info *tinfo = calloc(num_threads, sizeof(*tinfo));
352 handle_error("calloc");
354 /* Create one thread for each command\-line argument. */
356 for (int tnum = 0; tnum < num_threads; tnum++) {
357 tinfo[tnum].thread_num = tnum + 1;
358 tinfo[tnum].argv_string = argv[optind + tnum];
360 /* The pthread_create() call stores the thread ID into
361 corresponding element of tinfo[]. */
363 s = pthread_create(&tinfo[tnum].thread_id, &attr,
364 &thread_start, &tinfo[tnum]);
366 handle_error_en(s, "pthread_create");
369 /* Destroy the thread attributes object, since it is no
372 s = pthread_attr_destroy(&attr);
374 handle_error_en(s, "pthread_attr_destroy");
376 /* Now join with each thread, and display its returned value. */
378 for (int tnum = 0; tnum < num_threads; tnum++) {
379 s = pthread_join(tinfo[tnum].thread_id, &res);
381 handle_error_en(s, "pthread_join");
383 printf("Joined with thread %d; returned value was %s\en",
384 tinfo[tnum].thread_num, (char *) res);
385 free(res); /* Free memory allocated by thread */
396 .BR pthread_attr_init (3),
397 .BR pthread_cancel (3),
398 .BR pthread_detach (3),
399 .BR pthread_equal (3),
400 .BR pthread_exit (3),
401 .BR pthread_getattr_np (3),
402 .BR pthread_join (3),
403 .BR pthread_self (3),
404 .BR pthread_setattr_default_np (3),