1 .\" Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by John Birrell.
15 .\" 4. Neither the name of the author nor the names of any co-contributors
16 .\" may be used to endorse or promote products derived from this software
17 .\" without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" $FreeBSD: src/share/man/man3/pthread.3,v 1.33 2009/04/01 08:08:25 trhodes Exp $
32 .\" $DragonFly: src/share/man/man3/pthread.3,v 1.5 2007/09/14 19:47:03 swildner Exp $
39 .Nd POSIX thread functions
45 POSIX threads are a set of functions that support applications with
46 requirements for multiple flows of control, called
49 Multithreading is used to improve the performance of a
52 The POSIX thread functions are summarized in this section in the following
55 .Bl -bullet -offset indent -compact
59 Attribute Object Routines
63 Condition Variable Routines
65 Read/Write Lock Routines
67 Per-Thread Context Routines
72 .Bl -tag -width indent
76 .Fa "pthread_t *thread" "const pthread_attr_t *attr"
77 .Fa "void *\*[lp]*start_routine\*[rp]\*[lp]void *\*[rp]" "void *arg"
80 Creates a new thread of execution.
83 .Fn pthread_cancel "pthread_t thread"
85 Cancels execution of a thread.
88 .Fn pthread_detach "pthread_t thread"
90 Marks a thread for deletion.
93 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
95 Compares two thread IDs.
98 .Fn pthread_exit "void *value_ptr"
100 Terminates the calling thread.
103 .Fn pthread_join "pthread_t thread" "void **value_ptr"
105 Causes the calling thread to wait for the termination of the specified thread.
108 .Fn pthread_kill "pthread_t thread" "int sig"
110 Delivers a signal to a specified thread.
113 .Fn pthread_once "pthread_once_t *once_control" "void \*[lp]*init_routine\*[rp]\*[lp]void\*[rp]"
115 Calls an initialization routine once.
118 .Fn pthread_self void
120 Returns the thread ID of the calling thread.
123 .Fn pthread_setcancelstate "int state" "int *oldstate"
125 Sets the current thread's cancelability state.
128 .Fn pthread_setcanceltype "int type" "int *oldtype"
130 Sets the current thread's cancelability type.
133 .Fn pthread_testcancel void
135 Creates a cancellation point in the calling thread.
138 .Fn pthread_yield void
140 Allows the scheduler to run another thread instead of the current one.
142 .Ss Attribute Object Routines
143 .Bl -tag -width indent
146 .Fn pthread_attr_destroy "pthread_attr_t *attr"
148 Destroy a thread attributes object.
151 .Fo pthread_attr_getinheritsched
152 .Fa "const pthread_attr_t *attr" "int *inheritsched"
155 Get the inherit scheduling attribute from a thread attributes object.
158 .Fo pthread_attr_getschedparam
159 .Fa "const pthread_attr_t *attr" "struct sched_param *param"
162 Get the scheduling parameter attribute from a thread attributes object.
165 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
167 Get the scheduling policy attribute from a thread attributes object.
170 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
172 Get the contention scope attribute from a thread attributes object.
175 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
177 Get the stack size attribute from a thread attributes object.
180 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
182 Get the stack address attribute from a thread attributes object.
185 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
187 Get the detach state attribute from a thread attributes object.
190 .Fn pthread_attr_init "pthread_attr_t *attr"
192 Initialize a thread attributes object with default values.
195 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
197 Set the inherit scheduling attribute in a thread attributes object.
200 .Fo pthread_attr_setschedparam
201 .Fa "pthread_attr_t *attr" "const struct sched_param *param"
204 Set the scheduling parameter attribute in a thread attributes object.
207 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
209 Set the scheduling policy attribute in a thread attributes object.
212 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
214 Set the contention scope attribute in a thread attributes object.
217 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
219 Set the stack size attribute in a thread attributes object.
222 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
224 Set the stack address attribute in a thread attributes object.
227 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
229 Set the detach state in a thread attributes object.
232 .Bl -tag -width indent
235 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
237 Destroy a mutex attributes object.
240 .Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *ceiling"
242 Obtain priority ceiling attribute of mutex attribute object.
245 .Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
247 Obtain protocol attribute of mutex attribute object.
250 .Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
252 Obtain the mutex type attribute in the specified mutex attributes object.
255 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
257 Initialize a mutex attributes object with default values.
260 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int ceiling"
262 Set priority ceiling attribute of mutex attribute object.
265 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
267 Set protocol attribute of mutex attribute object.
270 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
272 Set the mutex type attribute that is used when a mutex is created.
275 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
280 .Fo pthread_mutex_init
281 .Fa "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
284 Initialize a mutex with specified attributes.
287 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
289 Lock a mutex and block until it becomes available.
292 .Fo pthread_mutex_timedlock
293 .Fa "pthread_mutex_t *mutex" "const struct timespec *abstime"
296 Lock a mutex and block until it becomes available or until the timeout expires.
299 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
301 Try to lock a mutex, but do not block if the mutex is locked by another thread,
302 including the current thread.
305 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
309 .Ss Condition Variable Routines
310 .Bl -tag -width indent
313 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
315 Destroy a condition variable attributes object.
318 .Fn pthread_condattr_init "pthread_condattr_t *attr"
320 Initialize a condition variable attributes object with default values.
323 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
325 Unblock all threads currently blocked on the specified condition variable.
328 .Fn pthread_cond_destroy "pthread_cond_t *cond"
330 Destroy a condition variable.
333 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
335 Initialize a condition variable with specified attributes.
338 .Fn pthread_cond_signal "pthread_cond_t *cond"
340 Unblock at least one of the threads blocked on the specified condition variable.
343 .Fo pthread_cond_timedwait
344 .Fa "pthread_cond_t *cond" "pthread_mutex_t *mutex"
345 .Fa "const struct timespec *abstime"
348 Wait no longer than the specified time for a condition
349 and lock the specified mutex.
352 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
354 Wait for a condition and lock the specified mutex.
356 .Ss Read/Write Lock Routines
357 .Bl -tag -width indent
360 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
362 Destroy a read/write lock object.
365 .Fo pthread_rwlock_init
366 .Fa "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
369 Initialize a read/write lock object.
372 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
374 Lock a read/write lock for reading, blocking until the lock can be
378 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
380 Attempt to lock a read/write lock for reading, without blocking if the
384 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
386 Attempt to lock a read/write lock for writing, without blocking if the
390 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
392 Unlock a read/write lock.
395 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
397 Lock a read/write lock for writing, blocking until the lock can be
401 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
403 Destroy a read/write lock attribute object.
406 .Fo pthread_rwlockattr_getpshared
407 .Fa "const pthread_rwlockattr_t *attr" "int *pshared"
410 Retrieve the process shared setting for the read/write lock attribute
414 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
416 Initialize a read/write lock attribute object.
419 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
421 Set the process shared setting for the read/write lock attribute object.
423 .Ss Per-Thread Context Routines
424 .Bl -tag -width indent
427 .Fn pthread_key_create "pthread_key_t *key" "void \*[lp]*routine\*[rp]\*[lp]void *\*[rp]"
429 Create a thread-specific data key.
432 .Fn pthread_key_delete "pthread_key_t key"
434 Delete a thread-specific data key.
437 .Fn pthread_getspecific "pthread_key_t key"
439 Get the thread-specific value for the specified key.
442 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
444 Set the thread-specific value for the specified key.
447 .Bl -tag -width indent
451 .Fa "void \*[lp]*prepare\*[rp]\*[lp]void\*[rp]"
452 .Fa "void \*[lp]*parent\*[rp]\*[lp]void\*[rp]"
453 .Fa "void \*[lp]*child\*[rp]\*[lp]void\*[rp]"
456 Register fork handlers
459 .Fn pthread_cleanup_pop "int execute"
461 Remove the routine at the top of the calling thread's cancellation cleanup
462 stack and optionally invoke it.
465 .Fn pthread_cleanup_push "void \*[lp]*routine\*[rp]\*[lp]void *\*[rp]" "void *routine_arg"
467 Push the specified cancellation cleanup handler onto the calling thread's
470 .Sh IMPLEMENTATION NOTES
473 POSIX thread implementation is built in the library
475 which contains both thread-safe libc functions and the thread functions.
476 This library replaces
478 for threaded applications.
482 is built as part of a 'make world'. To disable the build of
484 you must supply the '-DNOLIBC_R' option to
491 specific option exists in
493 to simplify the linking of threaded processes.
495 links a threaded process against
500 .Xr pthread_atfork 3 ,
501 .Xr pthread_cancel 3 ,
502 .Xr pthread_cleanup_pop 3 ,
503 .Xr pthread_cleanup_push 3 ,
504 .Xr pthread_condattr_destroy 3 ,
505 .Xr pthread_condattr_init 3 ,
506 .Xr pthread_cond_broadcast 3 ,
507 .Xr pthread_cond_destroy 3 ,
508 .Xr pthread_cond_init 3 ,
509 .Xr pthread_cond_signal 3 ,
510 .Xr pthread_cond_timedwait 3 ,
511 .Xr pthread_cond_wait 3 ,
512 .Xr pthread_create 3 ,
513 .Xr pthread_detach 3 ,
514 .Xr pthread_equal 3 ,
516 .Xr pthread_getspecific 3 ,
518 .Xr pthread_key_delete 3 ,
520 .Xr pthread_mutexattr_destroy 3 ,
521 .Xr pthread_mutexattr_getprioceiling 3 ,
522 .Xr pthread_mutexattr_getprotocol 3 ,
523 .Xr pthread_mutexattr_gettype 3 ,
524 .Xr pthread_mutexattr_init 3 ,
525 .Xr pthread_mutexattr_setprioceiling 3 ,
526 .Xr pthread_mutexattr_setprotocol 3 ,
527 .Xr pthread_mutexattr_settype 3 ,
528 .Xr pthread_mutex_destroy 3 ,
529 .Xr pthread_mutex_init 3 ,
530 .Xr pthread_mutex_lock 3 ,
531 .Xr pthread_mutex_trylock 3 ,
532 .Xr pthread_mutex_unlock 3 ,
534 .Xr pthread_rwlockattr_destroy 3 ,
535 .Xr pthread_rwlockattr_getpshared 3 ,
536 .Xr pthread_rwlockattr_init 3 ,
537 .Xr pthread_rwlockattr_setpshared 3 ,
538 .Xr pthread_rwlock_destroy 3 ,
539 .Xr pthread_rwlock_init 3 ,
540 .Xr pthread_rwlock_rdlock 3 ,
541 .Xr pthread_rwlock_unlock 3 ,
542 .Xr pthread_rwlock_wrlock 3 ,
544 .Xr pthread_setcancelstate 3 ,
545 .Xr pthread_setcanceltype 3 ,
546 .Xr pthread_setspecific 3 ,
547 .Xr pthread_testcancel 3
549 The functions with the
558 The functions with the
562 suffix are non-portable extensions to POSIX threads.
564 The functions with the
566 prefix are extensions created by The Open Group as part of the