]> git.ipfire.org Git - thirdparty/man-pages.git/blame - man3/pthread_create.3
man*/: srcfix (Use .P instead of .PP or .LP)
[thirdparty/man-pages.git] / man3 / pthread_create.3
CommitLineData
a1eaacb1 1'\" t
058cd95f
MK
2.\" Copyright (c) 2008 Linux Foundation, written by Michael Kerrisk
3.\" <mtk.manpages@gmail.com>
4.\"
5fbde956 5.\" SPDX-License-Identifier: Linux-man-pages-copyleft
058cd95f 6.\"
4c1c5274 7.TH pthread_create 3 (date) "Linux man-pages (unreleased)"
058cd95f
MK
8.SH NAME
9pthread_create \- create a new thread
91707f82
AC
10.SH LIBRARY
11POSIX threads library
8fc3b2cf 12.RI ( libpthread ", " \-lpthread )
058cd95f
MK
13.SH SYNOPSIS
14.nf
15.B #include <pthread.h>
c6d039a3 16.P
71f5a50a
AC
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 );
058cd95f 21.fi
058cd95f
MK
22.SH DESCRIPTION
23The
24.BR pthread_create ()
2bb98c3f 25function starts a new thread in the calling process.
058cd95f
MK
26The new thread starts execution by invoking
27.IR start_routine ();
1ae6b2c7 28.I arg
058cd95f
MK
29is passed as the sole argument of
30.IR start_routine ().
c6d039a3 31.P
058cd95f 32The new thread terminates in one of the following ways:
cdede5cd 33.IP \[bu] 3
058cd95f
MK
34It calls
35.BR pthread_exit (3),
36specifying an exit status value that is available to another thread
37in the same process that calls
38.BR pthread_join (3).
cdede5cd 39.IP \[bu]
058cd95f
MK
40It returns from
41.IR start_routine ().
42This is equivalent to calling
43.BR pthread_exit (3)
44with the value supplied in the
45.I return
46statement.
cdede5cd 47.IP \[bu]
058cd95f
MK
48It is canceled (see
49.BR pthread_cancel (3)).
cdede5cd 50.IP \[bu]
058cd95f
MK
51Any of the threads in the process calls
52.BR exit (3),
53or the main thread performs a return from
54.IR main ().
55This causes the termination of all threads in the process.
c6d039a3 56.P
058cd95f
MK
57The
58.I attr
59argument points to a
60.I pthread_attr_t
61structure whose contents are used at thread creation time to
62determine attributes for the new thread;
63this structure is initialized using
64.BR pthread_attr_init (3)
65and related functions.
66If
67.I attr
68is NULL,
69then the thread is created with default attributes.
c6d039a3 70.P
058cd95f
MK
71Before returning, a successful call to
72.BR pthread_create ()
73stores the ID of the new thread in the buffer pointed to by
74.IR thread ;
75this identifier is used to refer to the thread
76in subsequent calls to other pthreads functions.
c6d039a3 77.P
058cd95f
MK
78The new thread inherits a copy of the creating thread's signal mask
79.RB ( pthread_sigmask (3)).
c5571b61 80The set of pending signals for the new thread is empty
058cd95f
MK
81.RB ( sigpending (2)).
82The new thread does not inherit the creating thread's
83alternate signal stack
84.RB ( sigaltstack (2)).
c6d039a3 85.P
058cd95f
MK
86The new thread inherits the calling thread's floating-point environment
87.RB ( fenv (3)).
c6d039a3 88.P
058cd95f
MK
89The initial value of the new thread's CPU-time clock is 0
90(see
91.BR pthread_getcpuclockid (3)).
0eb44391 92.\" CLOCK_THREAD_CPUTIME_ID in clock_gettime(2)
058cd95f
MK
93.SS Linux-specific details
94The new thread inherits copies of the calling thread's capability sets
95(see
96.BR capabilities (7))
97and CPU affinity mask (see
98.BR sched_setaffinity (2)).
99.SH RETURN VALUE
100On success,
101.BR pthread_create ()
102returns 0;
103on error, it returns an error number, and the contents of
1ae6b2c7 104.I *thread
058cd95f
MK
105are undefined.
106.SH ERRORS
107.TP
108.B EAGAIN
20cbd039
MK
109Insufficient resources to create another thread.
110.TP
111.B EAGAIN
112.\" NOTE! The following should match the description in fork(2)
113A system-imposed limit on the number of threads was encountered.
114There are a number of limits that may trigger this error: the
1ae6b2c7 115.B RLIMIT_NPROC
058cd95f
MK
116soft resource limit (set via
117.BR setrlimit (2)),
20cbd039 118which limits the number of processes and threads for a real user ID,
058cd95f 119was reached;
03518b19 120the kernel's system-wide limit on the number of processes and threads,
b49c2acb 121.IR /proc/sys/kernel/threads\-max ,
20cbd039
MK
122was reached (see
123.BR proc (5));
124or the maximum number of PIDs,
125.IR /proc/sys/kernel/pid_max ,
126was reached (see
127.BR proc (5)).
058cd95f
MK
128.TP
129.B EINVAL
130Invalid settings in
131.IR attr .
132.TP
133.\" FIXME . Test the following
134.B EPERM
135No permission to set the scheduling policy and parameters specified in
136.IR attr .
a7930e0d
ZL
137.SH ATTRIBUTES
138For an explanation of the terms used in this section, see
139.BR attributes (7).
140.TS
141allbox;
c466875e 142lbx lb lb
a7930e0d
ZL
143l l l.
144Interface Attribute Value
145T{
9e54434e
BR
146.na
147.nh
a7930e0d
ZL
148.BR pthread_create ()
149T} Thread safety MT-Safe
150.TE
3113c7f3 151.SH STANDARDS
4131356c
AC
152POSIX.1-2008.
153.SH HISTORY
154POSIX.1-2001.
058cd95f
MK
155.SH NOTES
156See
157.BR pthread_self (3)
158for further information on the thread ID returned in
1ae6b2c7 159.I *thread
058cd95f
MK
160by
161.BR pthread_create ().
162Unless real-time scheduling policies are being employed,
163after a call to
164.BR pthread_create (),
36546c38 165it is indeterminate which thread\[em]the caller or the new thread\[em]will
058cd95f 166next execute.
c6d039a3 167.P
058cd95f
MK
168A thread may either be
169.I joinable
170or
171.IR detached .
172If a thread is joinable, then another thread can call
173.BR pthread_join (3)
174to wait for the thread to terminate and fetch its exit status.
175Only when a terminated joinable thread has been joined are
176the last of its resources released back to the system.
177When a detached thread terminates,
178its resources are automatically released back to the system:
179it is not possible to join with the thread in order to obtain
180its exit status.
181Making a thread detached is useful for some types of daemon threads
182whose exit status the application does not need to care about.
183By default, a new thread is created in a joinable state, unless
184.I attr
185was set to create the thread in a detached state (using
186.BR pthread_attr_setdetachstate (3)).
c6d039a3 187.P
058cd95f 188Under the NPTL threading implementation, if the
1ae6b2c7 189.B RLIMIT_STACK
058cd95f 190soft resource limit
1ae6b2c7 191.I at the time the program started
058cd95f
MK
192has any value other than "unlimited",
193then it determines the default stack size of new threads.
194Using
195.BR pthread_attr_setstacksize (3),
196the stack size attribute can be explicitly set in the
197.I attr
198argument used to create a thread,
199in order to obtain a stack size other than the default.
a706f0e0 200If the
1ae6b2c7 201.B RLIMIT_STACK
8efada99
MK
202resource limit is set to "unlimited",
203a per-architecture value is used for the stack size.
204Here is the value for a few architectures:
205.RS
af6e958b
FB
206.TS
207allbox;
208lb lb
209l r.
210Architecture Default stack size
169d7283
MK
211i386 2 MB
212IA-64 32 MB
af6e958b
FB
213PowerPC 4 MB
214S/390 2 MB
169d7283
MK
215Sparc-32 2 MB
216Sparc-64 4 MB
af6e958b
FB
217x86_64 2 MB
218.TE
8efada99 219.RE
22cb459d
MK
220.SH BUGS
221In the obsolete LinuxThreads implementation,
222each of the threads in a process has a different process ID.
223This is in violation of the POSIX threads specification,
224and is the source of many other nonconformances to the standard; see
225.BR pthreads (7).
a14af333 226.SH EXAMPLES
058cd95f
MK
227The program below demonstrates the use of
228.BR pthread_create (),
229as well as a number of other functions in the pthreads API.
c6d039a3 230.P
058cd95f
MK
231In the following run,
232on a system providing the NPTL threading implementation,
233the stack size defaults to the value given by the
234"stack size" resource limit:
c6d039a3 235.P
058cd95f 236.in +4n
b8302363 237.EX
b43a3b30 238.RB "$" " ulimit \-s"
d58522c0 2398192 # The stack size limit is 8 MB (0x800000 bytes)
b43a3b30 240.RB "$" " ./a.out hola salut servus"
058cd95f
MK
241Thread 1: top of stack near 0xb7dd03b8; argv_string=hola
242Thread 2: top of stack near 0xb75cf3b8; argv_string=salut
243Thread 3: top of stack near 0xb6dce3b8; argv_string=servus
244Joined with thread 1; returned value was HOLA
245Joined with thread 2; returned value was SALUT
246Joined with thread 3; returned value was SERVUS
b8302363 247.EE
058cd95f 248.in
c6d039a3 249.P
ee8655b5 250In the next run, the program explicitly sets a stack size of 1\ MB (using
058cd95f
MK
251.BR pthread_attr_setstacksize (3))
252for the created threads:
c6d039a3 253.P
058cd95f 254.in +4n
b8302363 255.EX
b43a3b30 256.RB "$" " ./a.out \-s 0x100000 hola salut servus"
058cd95f
MK
257Thread 1: top of stack near 0xb7d723b8; argv_string=hola
258Thread 2: top of stack near 0xb7c713b8; argv_string=salut
259Thread 3: top of stack near 0xb7b703b8; argv_string=servus
260Joined with thread 1; returned value was HOLA
261Joined with thread 2; returned value was SALUT
262Joined with thread 3; returned value was SERVUS
b8302363 263.EE
058cd95f 264.in
9c330504 265.SS Program source
d84d0300 266\&
b0b6ab4e 267.\" SRC BEGIN (pthread_create.c)
e7d0bb47 268.EX
ad3868f0
AC
269#include <ctype.h>
270#include <errno.h>
058cd95f 271#include <pthread.h>
058cd95f
MK
272#include <stdio.h>
273#include <stdlib.h>
ad3868f0 274#include <string.h>
058cd95f 275#include <unistd.h>
fe5dba13 276\&
d1a71985 277#define handle_error_en(en, msg) \e
940c8ce2 278 do { errno = en; perror(msg); exit(EXIT_FAILURE); } while (0)
fe5dba13 279\&
d1a71985 280#define handle_error(msg) \e
940c8ce2 281 do { perror(msg); exit(EXIT_FAILURE); } while (0)
fe5dba13 282\&
058cd95f 283struct thread_info { /* Used as argument to thread_start() */
188cf22e 284 pthread_t thread_id; /* ID returned by pthread_create() */
058cd95f
MK
285 int thread_num; /* Application\-defined thread # */
286 char *argv_string; /* From command\-line argument */
287};
fe5dba13 288\&
058cd95f 289/* Thread start function: display address near top of our stack,
46b20ca1 290 and return upper\-cased copy of argv_string. */
fe5dba13 291\&
058cd95f
MK
292static void *
293thread_start(void *arg)
294{
13f78d96 295 struct thread_info *tinfo = arg;
88893a77 296 char *uargv;
fe5dba13 297\&
d1a71985 298 printf("Thread %d: top of stack near %p; argv_string=%s\en",
d917c31d 299 tinfo\->thread_num, (void *) &tinfo, tinfo\->argv_string);
fe5dba13 300\&
058cd95f
MK
301 uargv = strdup(tinfo\->argv_string);
302 if (uargv == NULL)
940c8ce2 303 handle_error("strdup");
fe5dba13 304\&
b957f81f 305 for (char *p = uargv; *p != \[aq]\e0\[aq]; p++)
058cd95f 306 *p = toupper(*p);
fe5dba13 307\&
058cd95f
MK
308 return uargv;
309}
fe5dba13 310\&
058cd95f
MK
311int
312main(int argc, char *argv[])
313{
b42296e4 314 int s, opt;
0f6f10d5 315 void *res;
b42296e4 316 size_t num_threads;
0f6f10d5
AC
317 ssize_t stack_size;
318 pthread_attr_t attr;
319 struct thread_info *tinfo;
fe5dba13 320\&
46b20ca1 321 /* The "\-s" option specifies a stack size for our threads. */
fe5dba13 322\&
058cd95f
MK
323 stack_size = \-1;
324 while ((opt = getopt(argc, argv, "s:")) != \-1) {
325 switch (opt) {
b957f81f 326 case \[aq]s\[aq]:
058cd95f
MK
327 stack_size = strtoul(optarg, NULL, 0);
328 break;
fe5dba13 329\&
058cd95f 330 default:
d064d41a 331 fprintf(stderr, "Usage: %s [\-s stack\-size] arg...\en",
058cd95f
MK
332 argv[0]);
333 exit(EXIT_FAILURE);
334 }
335 }
fe5dba13 336\&
058cd95f 337 num_threads = argc \- optind;
fe5dba13 338\&
46b20ca1 339 /* Initialize thread creation attributes. */
fe5dba13 340\&
058cd95f
MK
341 s = pthread_attr_init(&attr);
342 if (s != 0)
940c8ce2 343 handle_error_en(s, "pthread_attr_init");
fe5dba13 344\&
058cd95f
MK
345 if (stack_size > 0) {
346 s = pthread_attr_setstacksize(&attr, stack_size);
347 if (s != 0)
940c8ce2 348 handle_error_en(s, "pthread_attr_setstacksize");
058cd95f 349 }
fe5dba13 350\&
46b20ca1 351 /* Allocate memory for pthread_create() arguments. */
fe5dba13 352\&
0f6f10d5 353 tinfo = calloc(num_threads, sizeof(*tinfo));
058cd95f 354 if (tinfo == NULL)
940c8ce2 355 handle_error("calloc");
fe5dba13 356\&
46b20ca1 357 /* Create one thread for each command\-line argument. */
fe5dba13 358\&
b42296e4 359 for (size_t tnum = 0; tnum < num_threads; tnum++) {
058cd95f
MK
360 tinfo[tnum].thread_num = tnum + 1;
361 tinfo[tnum].argv_string = argv[optind + tnum];
fe5dba13 362\&
058cd95f 363 /* The pthread_create() call stores the thread ID into
46b20ca1 364 corresponding element of tinfo[]. */
fe5dba13 365\&
058cd95f
MK
366 s = pthread_create(&tinfo[tnum].thread_id, &attr,
367 &thread_start, &tinfo[tnum]);
368 if (s != 0)
940c8ce2 369 handle_error_en(s, "pthread_create");
058cd95f 370 }
fe5dba13 371\&
058cd95f 372 /* Destroy the thread attributes object, since it is no
46b20ca1 373 longer needed. */
fe5dba13 374\&
058cd95f
MK
375 s = pthread_attr_destroy(&attr);
376 if (s != 0)
940c8ce2 377 handle_error_en(s, "pthread_attr_destroy");
fe5dba13 378\&
46b20ca1 379 /* Now join with each thread, and display its returned value. */
fe5dba13 380\&
b42296e4 381 for (size_t tnum = 0; tnum < num_threads; tnum++) {
058cd95f
MK
382 s = pthread_join(tinfo[tnum].thread_id, &res);
383 if (s != 0)
940c8ce2 384 handle_error_en(s, "pthread_join");
fe5dba13 385\&
d1a71985 386 printf("Joined with thread %d; returned value was %s\en",
d917c31d 387 tinfo[tnum].thread_num, (char *) res);
058cd95f
MK
388 free(res); /* Free memory allocated by thread */
389 }
fe5dba13 390\&
2bb98c3f 391 free(tinfo);
058cd95f
MK
392 exit(EXIT_SUCCESS);
393}
e7d0bb47 394.EE
b0b6ab4e 395.\" SRC END
058cd95f 396.SH SEE ALSO
ca8a0bd2
MK
397.ad l
398.nh
058cd95f
MK
399.BR getrlimit (2),
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),
c09bc825 408.BR pthread_setattr_default_np (3),
058cd95f 409.BR pthreads (7)