2 .\" Copyright (c) 2009 Sun Microsystems, Inc. All Rights Reserved.
3 .\" Portions Copyright (c) 2001, the Institute of Electrical and Electronics Engineers, Inc. and The Open Group. All Rights Reserved.
4 .\" Portions Copyright (c) 1995 IEEE. All Rights Reserved.
5 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for permission to reproduce portions of its copyrighted documentation. Original documentation from The Open Group can be obtained online at http://www.opengroup.org/bookstore/.
6 .\" The Institute of Electrical and Electronics Engineers and The Open Group, have given us permission to reprint portions of their documentation. In the following statement, the phrase "this text" refers to portions of the system documentation. Portions of this text
7 .\" are reprinted and reproduced in electronic form in the Sun OS Reference Manual, from IEEE Std 1003.1, 2004 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 6, Copyright (C) 2001-2004 by the Institute of Electrical
8 .\" and Electronics Engineers, Inc and The Open Group. In the event of any discrepancy between these versions and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at http://www.opengroup.org/unix/online.html.
9 .\" This notice shall appear on any product containing this material.
10 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License"). You may not use this file except in compliance with the License. You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.
11 .\" See the License for the specific language governing permissions and limitations under the License. When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE. If applicable, add the following below this CDDL HEADER, with
12 .\" the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
13 .TH THR_CREATE 3C "Mar 16, 2009"
15 thr_create \- create a thread
19 cc -mt [ \fIflag\fR... ] \fIfile\fR...[ \fIlibrary\fR... ]
22 \fBint\fR \fBthr_create\fR(\fBvoid *\fR\fIstack_base\fR, \fBsize_t\fR \fIstack_size\fR,
23 \fBvoid *(*\fR\fIstart_func\fR) (void*), \fBvoid *\fR\fIarg\fR, \fBlong\fR \fIflags\fR,
24 \fBthread_t *\fR\fInew_thread_ID\fR);
30 Thread creation adds a new thread of control to the current process. The
31 procedure \fBmain()\fR is a single thread of control. Each thread executes
32 concurrently with all other threads within the calling process and with other
33 threads from other active processes.
36 Although a newly created thread shares all of the calling process's global data
37 with the other threads in the process, it has its own set of attributes and
38 private execution stack. The new thread inherits the calling thread's signal
39 mask and scheduling priority. Pending signals for a new thread are not
40 inherited and will be empty.
43 The call to create a thread takes the address of a user-defined function,
44 specified by \fIstart_func\fR, as one of its arguments. This function is the
45 complete execution routine for the new thread.
48 The lifetime of a thread begins with the successful return from
49 \fBthr_create()\fR, which calls \fIstart_func\fR(\|) and ends with one of the
55 the normal completion of \fIstart_func\fR(\|),
61 an explicit call to \fBthr_exit\fR(3C), or
67 the conclusion of the calling process (see \fBexit\fR(2)).
71 The new thread performs by calling the function defined by \fIstart_func\fR
72 with only one argument, \fIarg\fR. If more than one argument needs to be passed
73 to \fIstart_func\fR, the arguments can be packed into a structure, the address
74 of which can be passed to \fIarg\fR.
77 If \fIstart_func\fR returns, the thread terminates with the exit status set to
78 the \fIstart_func\fR return value (see \fBthr_exit\fR(3C)).
81 When the thread from which \fBmain()\fR originated returns, the effect is the
82 same as if an implicit call to \fBexit()\fR were made using the return value of
83 \fBmain()\fR as the exit status. This behavior differs from a \fIstart_func\fR
84 return. If \fBmain()\fR calls \fBthr_exit\fR(3C), only the \fBmain\fR thread
85 exits, not the entire process.
88 If the thread creation fails, a new thread is not created and the contents of
89 the location referenced by the pointer to the new thread are undefined.
92 The \fIflags\fR argument specifies which attributes are modifiable for the
93 created thread. The value in \fIflags\fR is determined by the bitwise
94 inclusive-OR of the following:
98 \fB\fBTHR_BOUND\fR \fR
101 This flag is obsolete and is maintained for compatibility.
107 \fB\fBTHR_DETACHED\fR \fR
110 This flag affects the detachstate attribute of the thread. The new thread is
111 created detached. The exit status of a detached thread is not accessible to
112 other threads. Its thread ID and other resources may be re-used as soon as the
113 thread terminates. \fBthr_join\fR(3C) will not wait for a detached thread.
119 \fB\fBTHR_NEW_LWP\fR \fR
122 This flag is obsolete and is maintained for compatibility.
128 \fB\fBTHR_SUSPENDED\fR \fR
131 This flag affects the suspended attribute of the thread. The new thread is
132 created suspended and will not execute \fIstart_func\fR until it is started by
133 \fBthr_continue()\fR.
139 \fB\fBTHR_DAEMON\fR \fR
142 This flag affects the daemon attribute of the thread. In addition to being
143 created detached (\fBTHR_DAEMON\fR implies \fBTHR_DETACHED\fR), the thread is
144 marked as a daemon. Daemon threads do not interfere with the exit conditions
145 for a process. A process will terminate when the last non-daemon thread exits
146 or the process calls \fBexit\fR(2). Also, a thread that is waiting in
147 \fBthr_join\fR(3C) for any thread to terminate will return \fBEDEADLK\fR when
148 all remaining threads in the process are either daemon threads or other threads
149 waiting in \fBthr_join()\fR. Daemon threads are most useful in libraries that
155 Default thread creation:
160 void *start_func(void *), *arg;
161 thr_create(NULL, 0, start_func, arg, 0, &tid);
167 Create a detached thread whose thread ID we do not care about:
171 thr_create(NULL, 0, start_func, arg, THR_DETACHED, NULL);
177 If \fIstack_base\fR is not \fINULL\fR, the new thread uses the stack beginning
178 at the address specified by \fIstack_base\fR and continuing for
179 \fIstack_size\fR bytes, where \fIstack_size\fR must be greater than or equal to
180 \fBTHR_MIN_STACK\fR. If \fIstack_base\fR is \fINULL\fR, \fBthr_create()\fR
181 allocates a stack for the new thread with at least \fIstack_size\fR bytes. If
182 \fIstack_size\fR is 0, a default size is used. If \fIstack_size\fR is not 0, it
183 must be greater than or equal to \fBTHR_MIN_STACK\fR. See \fBNOTES\fR.
186 When \fInew_thread_ID\fR is not \fINULL\fR, it points to a location where the
187 \fBID\fR of the new thread is stored if \fBthr_create()\fR is successful. The
188 \fBID\fR is only valid within the calling process.
192 If successful, the \fBthr_create()\fR function returns \fB0\fR. Otherwise, an
193 error value is returned to indicate the error.
201 A resource control limit on the total number of threads in a process, task,
202 project, or zone has been exceeded or some system resource has been exceeded.
211 The \fIstack_base\fR argument is not \fINULL\fR and \fIstack_size\fR is less
212 than \fBTHR_MIN_STACK\fR, or the \fIstack_base\fR argument is \fINULL\fR and
213 \fIstack_size\fR is not \fB0\fR and is less than \fBTHR_MIN_STACK\fR.
222 The system cannot allocate stack for the thread.
227 The \fBthr_create()\fR function may use \fBmmap()\fR to allocate thread stacks
228 from \fBMAP_PRIVATE\fR, \fBMAP_NORESERVE\fR, and \fBMAP_ANON\fR memory mappings
229 if \fIstack_base\fR is \fINULL\fR, and consequently may return upon failure the
230 relevant error values returned by \fBmmap()\fR. See the \fBmmap\fR(2) manual
231 page for these error values.
235 The following is an example of concurrency with multithreading. Since POSIX
236 threads and Solaris threads are fully compatible even within the same process,
237 this example uses \fBpthread_create()\fR if you execute \fBa.out 0\fR, or
238 \fBthr_create()\fR if you execute \fBa.out 1\fR.
241 Five threads are created that simultaneously perform a time-consuming function,
242 \fBsleep(\fR10\fB)\fR. If the execution of this process is timed, the results
243 will show that all five individual calls to sleep for ten-seconds completed
244 in about ten seconds, even on a uniprocessor. If a single-threaded process
245 calls \fBsleep(\fR10\fB)\fR five times, the execution time will be about
249 The command-line to time this process is:
252 \fB/usr/bin/time\fR \fBa.out\fR \fB0\fR \fB(for\fR \fBPOSIX\fR \fBthreading)\fR
258 \fB/usr/bin/time\fR \fBa.out\fR \fB1\fR \fB(for\fR \fBSolaris\fR
261 \fBExample 1 \fRAn example of concurrency with multithreading.
265 #define _REENTRANT /* basic 3-lines for threads */
268 #define NUM_THREADS 5
269 #define SLEEP_TIME 10
271 void *sleeping(void *); /* thread routine */
273 thread_t tid[NUM_THREADS]; /* array of thread IDs */
276 main(int argc, char *argv[])
279 printf("use 0 as arg1 to use pthread_create(\|)\en");
280 printf("or use 1 as arg1 to use thr_create(\|)\en");
285 case '0': /* POSIX */
286 for ( i = 0; i < NUM_THREADS; i++)
287 pthread_create(&tid[i], NULL, sleeping,
289 for ( i = 0; i < NUM_THREADS; i++)
290 pthread_join(tid[i], NULL);
293 case '1': /* Solaris */
294 for ( i = 0; i < NUM_THREADS; i++)
295 thr_create(NULL, 0, sleeping, (void *)SLEEP_TIME, 0,
297 while (thr_join(0, NULL, NULL) == 0)
301 printf("main(\|) reporting that all %d threads have
309 int sleep_time = (int)arg;
310 printf("thread %d sleeping %d seconds ...\en", thr_self(\|),
313 printf("\enthread %d awakening\en", thr_self(\|));
321 Had \fBmain()\fR not waited for the completion of the other threads (using
322 \fBpthread_join\fR(3C) or \fBthr_join\fR(3C)), it would have continued to
323 process concurrently until it reached the end of its routine and the entire
324 process would have exited prematurely (see \fBexit\fR(2)).
327 \fBExample 2 \fRCreating a default thread with a new signal mask.
330 The following example demonstrates how to create a default thread with a new
331 signal mask. The \fInew_mask\fR argument is assumed to have a value different
332 from the creator's signal mask (\fIorig_mask\fR). The \fInew_mask\fR argument
333 is set to block all signals except for \fBSIGINT\fR. The creator's signal mask
334 is changed so that the new thread inherits a different mask, and is restored to
335 its original value after \fBthr_create()\fR returns.
339 This example assumes that \fBSIGINT\fR is also unmasked in the creator. If it
340 is masked by the creator, then unmasking the signal opens the creator to this
341 signal. The other alternative is to have the new thread set its own signal
342 mask in its start routine.
348 sigset_t new_mask, orig_mask;
351 (void)sigfillset(&new_mask);
352 (void)sigdelset(&new_mask, SIGINT);
353 (void)thr_sigsetmask(SIG_SETMASK, &new_mask, &orig_mask);
354 error = thr_create(NULL, 0, do_func, NULL, 0, &tid);
355 (void)thr_sigsetmask(SIG_SETMASK, &orig_mask, NULL);
362 See \fBattributes\fR(5) for descriptions of the following attributes:
370 ATTRIBUTE TYPE ATTRIBUTE VALUE
378 \fBexit\fR(2), \fBgetrlimit\fR(2), \fBmmap\fR(2), \fBexit\fR(3C),
379 \fBsleep\fR(3C), \fBthr_exit\fR(3C), \fBthr_join\fR(3C),
380 \fBthr_min_stack\fR(3C), \fBthr_setconcurrency\fR(3C), \fBthr_suspend\fR(3C),
381 \fBattributes\fR(5), \fBstandards\fR(5), \fBthreads\fR(5)
385 Since multithreaded-application threads execute independently of each other,
386 their relative behavior is unpredictable. It is therefore possible for the
387 thread executing \fBmain()\fR to finish before all other user-application
391 Using \fBthr_join\fR(3C) in the following syntax,
395 while (thr_join(0, NULL, NULL) == 0)
402 will cause the invoking thread (which may be \fBmain()\fR) to wait for the
403 termination of all non-daemon threads, excluding threads that are themselves
404 waiting in \fBthr_join()\fR; however, the second and third arguments to
405 \fBthr_join()\fR need not necessarily be \fINULL\fR.
408 A thread has not terminated until \fBthr_exit()\fR has finished. The only way
409 to determine this is by \fBthr_join()\fR. When \fBthr_join()\fR returns a
410 departed thread, it means that this thread has terminated and its resources are
411 reclaimable. For instance, if a user specified a stack to \fBthr_create()\fR,
412 this stack can only be reclaimed after \fBthr_join()\fR has reported this
413 thread as a departed thread. It is not possible to determine when a
414 \fIdetached\fR thread has terminated. A detached thread disappears without
418 Typically, thread stacks allocated by \fBthr_create()\fR begin on page
419 boundaries and any specified (a red-zone) size is rounded up to the next page
420 boundary. A page with no access permission is appended to the top of the stack
421 so that most stack overflows will result in a \fBSIGSEGV\fR signal being sent
422 to the offending thread. Thread stacks allocated by the caller are used as is.
425 Using a default stack size for the new thread, instead of passing a
426 user-specified stack size, results in much better \fBthr_create()\fR
427 performance. The default stack size for a user-thread is 1 megabyte in a
428 32-bit process and 2 megabyte in a 64-bit process.
431 A user-specified stack size must be greater than or equal to
432 \fBTHR_MIN_STACK\fR. A minimum stack size may not accommodate the stack frame
433 for the user thread function \fIstart_func\fR. If a stack size is specified,
434 it must accommodate \fIstart_func\fR requirements and the functions that it may
435 call in turn, in addition to the minimum requirement.
438 It is usually very difficult to determine the runtime stack requirements for a
439 thread. \fBTHR_MIN_STACK\fR specifies how much stack storage is required to
440 execute a trivial \fIstart_func\fR. The total runtime requirements for stack
441 storage are dependent on the storage required to do runtime linking, the amount
442 of storage required by library runtimes (like \fBprintf()\fR) that your thread
443 calls. Since these storage parameters are not known before the program runs, it
444 is best to use default stacks. If you know your runtime requirements or decide
445 to use stacks that are larger than the default, then it makes sense to specify