1 .\" Portions Copyright (c) 2001 Apple Computer, Inc. All Rights Reserved.
2 .\" Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by John Birrell.
16 .\" 4. Neither the name of the author nor the names of any co-contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 .Nd POSIX thread functions
43 POSIX threads are a set of functions that support applications with
44 requirements for multiple flows of control, called
47 Multithreading is used to improve the performance of a
50 The POSIX thread functions are summarized in this section in the following
53 .Bl -bullet -offset indent -compact
57 Attribute Object Routines
61 Condition Variable Routines
63 Read/Write Lock Routines
65 Per-Thread Context Routines
70 .Bl -tag -width indent
74 .Fa "pthread_t *thread" "const pthread_attr_t *attr"
75 .Fa "void *\*[lp]*start_routine\*[rp]\*[lp]void *\*[rp]" "void *arg"
78 Creates a new thread of execution.
81 .Fn pthread_cancel "pthread_t thread"
83 Cancels execution of a thread.
86 .Fn pthread_detach "pthread_t thread"
88 Marks a thread for deletion.
91 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
93 Compares two thread IDs.
96 .Fn pthread_exit "void *value_ptr"
98 Terminates the calling thread.
101 .Fn pthread_join "pthread_t thread" "void **value_ptr"
103 Causes the calling thread to wait for the termination of the specified thread.
106 .Fn pthread_kill "pthread_t thread" "int sig"
108 Delivers a signal to a specified thread.
111 .Fn pthread_once "pthread_once_t *once_control" "void \*[lp]*init_routine\*[rp]\*[lp]void\*[rp]"
113 Calls an initialization routine once.
116 .Fn pthread_self void
118 Returns the thread ID of the calling thread.
121 .Fn pthread_setcancelstate "int state" "int *oldstate"
123 Sets the current thread's cancelability state.
126 .Fn pthread_setcanceltype "int type" "int *oldtype"
128 Sets the current thread's cancelability type.
131 .Fn pthread_testcancel void
133 Creates a cancellation point in the calling thread.
135 .Ss Attribute Object Routines
136 .Bl -tag -width indent
139 .Fn pthread_attr_destroy "pthread_attr_t *attr"
141 Destroy a thread attributes object.
144 .Fo pthread_attr_getinheritsched
145 .Fa "const pthread_attr_t *attr" "int *inheritsched"
148 Get the inherit scheduling attribute from a thread attributes object.
151 .Fo pthread_attr_getschedparam
152 .Fa "const pthread_attr_t *attr" "struct sched_param *param"
155 Get the scheduling parameter attribute from a thread attributes object.
158 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
160 Get the scheduling policy attribute from a thread attributes object.
163 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
165 Get the contention scope attribute from a thread attributes object.
168 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
170 Get the stack size attribute from a thread attributes object.
173 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
175 Get the stack address attribute from a thread attributes object.
178 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
180 Get the detach state attribute from a thread attributes object.
183 .Fn pthread_attr_init "pthread_attr_t *attr"
185 Initialize a thread attributes object with default values.
188 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
190 Set the inherit scheduling attribute in a thread attributes object.
193 .Fo pthread_attr_setschedparam
194 .Fa "pthread_attr_t *attr" "const struct sched_param *param"
197 Set the scheduling parameter attribute in a thread attributes object.
200 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
202 Set the scheduling policy attribute in a thread attributes object.
205 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
207 Set the contention scope attribute in a thread attributes object.
210 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
212 Set the stack size attribute in a thread attributes object.
215 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
217 Set the stack address attribute in a thread attributes object.
220 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
222 Set the detach state in a thread attributes object.
225 .Bl -tag -width indent
228 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
230 Destroy a mutex attributes object.
233 .Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *ceiling"
235 Obtain priority ceiling attribute of mutex attribute object.
238 .Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
240 Obtain protocol attribute of mutex attribute object.
243 .Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
245 Obtain the mutex type attribute in the specified mutex attributes object.
248 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
250 Initialize a mutex attributes object with default values.
253 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int ceiling"
255 Set priority ceiling attribute of mutex attribute object.
258 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
260 Set protocol attribute of mutex attribute object.
263 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
265 Set the mutex type attribute that is used when a mutex is created.
268 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
273 .Fo pthread_mutex_init
274 .Fa "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
277 Initialize a mutex with specified attributes.
280 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
282 Lock a mutex and block until it becomes available.
285 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
287 Try to lock a mutex, but do not block if the mutex is locked by another thread,
288 including the current thread.
291 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
295 .Ss Condition Variable Routines
296 .Bl -tag -width indent
299 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
301 Destroy a condition variable attributes object.
304 .Fn pthread_condattr_init "pthread_condattr_t *attr"
306 Initialize a condition variable attributes object with default values.
309 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
311 Unblock all threads currently blocked on the specified condition variable.
314 .Fn pthread_cond_destroy "pthread_cond_t *cond"
316 Destroy a condition variable.
319 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
321 Initialize a condition variable with specified attributes.
324 .Fn pthread_cond_signal "pthread_cond_t *cond"
326 Unblock at least one of the threads blocked on the specified condition variable.
329 .Fo pthread_cond_timedwait
330 .Fa "pthread_cond_t *cond" "pthread_mutex_t *mutex"
331 .Fa "const struct timespec *abstime"
334 Unlock the specified mutex, wait no longer than the specified time for
335 a condition, and then relock the mutex.
338 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
340 Unlock the specified mutex, wait for a condition, and relock the mutex.
342 .Ss Read/Write Lock Routines
343 .Bl -tag -width indent
346 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
348 Destroy a read/write lock object.
351 .Fo pthread_rwlock_init
352 .Fa "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
355 Initialize a read/write lock object.
358 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
360 Lock a read/write lock for reading, blocking until the lock can be
364 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
366 Attempt to lock a read/write lock for reading, without blocking if the
370 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
372 Attempt to lock a read/write lock for writing, without blocking if the
376 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
378 Unlock a read/write lock.
381 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
383 Lock a read/write lock for writing, blocking until the lock can be
387 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
389 Destroy a read/write lock attribute object.
392 .Fo pthread_rwlockattr_getpshared
393 .Fa "const pthread_rwlockattr_t *attr" "int *pshared"
396 Retrieve the process shared setting for the read/write lock attribute
400 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
402 Initialize a read/write lock attribute object.
405 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
407 Set the process shared setting for the read/write lock attribute object.
409 .Ss Per-Thread Context Routines
410 .Bl -tag -width indent
413 .Fn pthread_key_create "pthread_key_t *key" "void \*[lp]*routine\*[rp]\*[lp]void *\*[rp]"
415 Create a thread-specific data key.
418 .Fn pthread_key_delete "pthread_key_t key"
420 Delete a thread-specific data key.
423 .Fn pthread_getspecific "pthread_key_t key"
425 Get the thread-specific value for the specified key.
428 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
430 Set the thread-specific value for the specified key.
433 .Bl -tag -width indent
437 .Fa "void \*[lp]*prepare\*[rp]\*[lp]void\*[rp]"
438 .Fa "void \*[lp]*parent\*[rp]\*[lp]void\*[rp]"
439 .Fa "void \*[lp]*child\*[rp]\*[lp]void\*[rp]"
442 Register fork handlers.
445 .Fn pthread_cleanup_pop "int execute"
447 Remove the routine at the top of the calling thread's cancellation cleanup
448 stack and optionally invoke it.
451 .Fn pthread_cleanup_push "void \*[lp]*routine\*[rp]\*[lp]void *\*[rp]" "void *routine_arg"
453 Push the specified cancellation cleanup handler onto the calling thread's
457 The default system libraries include
460 No additional libraries or CFLAGS are necessary to use these interfaces.
462 .Xr pthread_atfork 3 ,
464 .Xr pthread_cancel 3 ,
465 .Xr pthread_cleanup_pop 3 ,
466 .Xr pthread_cleanup_push 3 ,
467 .Xr pthread_cond_broadcast 3 ,
468 .Xr pthread_cond_destroy 3 ,
469 .Xr pthread_cond_init 3 ,
470 .Xr pthread_cond_signal 3 ,
471 .Xr pthread_cond_timedwait 3 ,
472 .Xr pthread_cond_wait 3 ,
473 .Xr pthread_condattr 3 ,
474 .Xr pthread_condattr_destroy 3 ,
475 .Xr pthread_condattr_init 3 ,
476 .Xr pthread_create 3 ,
477 .Xr pthread_detach 3 ,
478 .Xr pthread_equal 3 ,
480 .Xr pthread_getschedparam 3 ,
481 .Xr pthread_getspecific 3 ,
483 .Xr pthread_key_create 3 ,
484 .Xr pthread_key_delete 3 ,
486 .Xr pthread_mutex_destroy 3 ,
487 .Xr pthread_mutex_init 3 ,
488 .Xr pthread_mutex_lock 3 ,
489 .Xr pthread_mutex_trylock 3 ,
490 .Xr pthread_mutex_unlock 3 ,
491 .Xr pthread_mutexattr 3 ,
492 .Xr pthread_mutexattr_destroy 3 ,
493 .Xr pthread_mutexattr_getprioceiling 3 ,
494 .Xr pthread_mutexattr_getprotocol 3 ,
495 .Xr pthread_mutexattr_gettype 3 ,
496 .Xr pthread_mutexattr_init 3 ,
497 .Xr pthread_mutexattr_setprioceiling 3 ,
498 .Xr pthread_mutexattr_setprotocol 3 ,
499 .Xr pthread_mutexattr_settype 3 ,
501 .Xr pthread_rwlock_destroy 3 ,
502 .Xr pthread_rwlock_init 3 ,
503 .Xr pthread_rwlock_rdlock 3 ,
504 .Xr pthread_rwlock_tryrdlock 3 ,
505 .Xr pthread_rwlock_trywrlock 3 ,
506 .Xr pthread_rwlock_unlock 3 ,
507 .Xr pthread_rwlock_wrlock 3 ,
508 .Xr pthread_rwlockattr_destroy 3 ,
509 .Xr pthread_rwlockattr_getpshared 3 ,
510 .Xr pthread_rwlockattr_init 3 ,
511 .Xr pthread_rwlockattr_setpshared 3 ,
513 .Xr pthread_setcancelstate 3 ,
514 .Xr pthread_setcanceltype 3 ,
515 .Xr pthread_setspecific 3 ,
516 .Xr pthread_testcancel 3
518 The functions with the
527 The functions with the
531 suffix are non-portable extensions to POSIX threads.
533 The functions with the
535 prefix are extensions created by The Open Group as part of the