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.12.2.6 2002/03/25 08:28:18 maxim Exp $
32 .\" $DragonFly: src/share/man/man3/pthread.3,v 1.4 2004/05/10 13:11:12 hmp Exp $
34 .Dd September 10, 1998
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
52 .Bl -bullet -offset indent
56 Attribute Object Routines
60 Condition Variable Routines
62 Read/Write Lock Routines
64 Per-Thread Context Routines
72 .Fn pthread_create "pthread_t *thread" "const pthread_attr_t *attr" "void *(*start_routine)(void *)" "void *arg"
74 Creates a new thread of execution.
77 .Fn pthread_cancel "pthread_t thread"
79 Cancels execution of a thread.
82 .Fn pthread_detach "pthread_t thread"
84 Marks a thread for deletion.
87 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
89 Compares two thread IDs.
92 .Fn pthread_exit "void *value_ptr"
94 Terminates the calling thread.
97 .Fn pthread_join "pthread_t thread" "void **value_ptr"
99 Causes the calling thread to wait for the termination of the specified thread.
102 .Fn pthread_kill "pthread_t thread" "int sig"
104 Delivers a signal to a specified thread.
107 .Fn pthread_once "pthread_once_t *once_control" "void (*init_routine)(void)"
109 Calls an initialization routine once.
112 .Fn pthread_self void
114 Returns the thread ID of the calling thread.
117 .Fn pthread_setcancelstate "int state" "int *oldstate"
119 Sets the current thread's cancelability state.
122 .Fn pthread_setcanceltype "int type" "int *oldtype"
124 Sets the current thread's cancelability type.
127 .Fn pthread_testcancel void
129 Creates a cancellation point in the calling thread.
132 .Fn pthread_yield void
134 Allows the scheduler to run another thread instead of the current one.
136 .Sh ATTRIBUTE OBJECT ROUTINES
140 .Fn pthread_attr_destroy "pthread_attr_t *attr"
142 Destroy a thread attributes object.
145 .Fn pthread_attr_getinheritsched "const pthread_attr_t *attr" "int *inheritsched"
147 Get the inherit scheduling attribute from a thread attributes object.
150 .Fn pthread_attr_getschedparam "const pthread_attr_t *attr" "struct sched_param *param"
152 Get the scheduling parameter attribute from a thread attributes object.
155 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
157 Get the scheduling policy attribute from a thread attributes object.
160 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
162 Get the contention scope attribute from a thread attributes object.
165 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
167 Get the stack size attribute from a thread attributes object.
170 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
172 Get the stack address attribute from a thread attributes object.
175 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
177 Get the detach state attribute from a thread attributes object.
180 .Fn pthread_attr_init "pthread_attr_t *attr"
182 Initialize a thread attributes object with default values.
185 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
187 Set the inherit scheduling attribute in a thread attributes object.
190 .Fn pthread_attr_setschedparam "pthread_attr_t *attr" "const struct sched_param *param"
192 Set the scheduling parameter attribute in a thread attributes object.
195 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
197 Set the scheduling policy attribute in a thread attributes object.
200 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
202 Set the contention scope attribute in a thread attributes object.
205 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
207 Set the stack size attribute in a thread attributes object.
210 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
212 Set the stack address attribute in a thread attributes object.
215 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
217 Set the detach state in a thread attributes object.
223 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
225 Destroy a mutex attributes object.
228 .Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *ceiling"
230 Obtain priority ceiling attribute of mutex attribute object.
233 .Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
235 Obtain protocol attribute of mutex attribute object.
238 .Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
240 Obtain the mutex type attribute in the specified mutex attributes object.
243 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
245 Initialize a mutex attributes object with default values.
248 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int ceiling"
250 Set priority ceiling attribute of mutex attribute object.
253 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
255 Set protocol attribute of mutex attribute object.
258 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
260 Set the mutex type attribute that is used when a mutex is created.
263 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
268 .Fn pthread_mutex_init "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
270 Initialize a mutex with specified attributes.
273 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
275 Lock a mutex and block until it becomes available.
278 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
280 Try to lock a mutex, but don't block if the mutex is locked by another thread,
281 including the current thread.
284 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
288 .Sh CONDITION VARIABLE ROUTINES
292 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
294 Destroy a condition variable attributes object.
297 .Fn pthread_condattr_init "pthread_condattr_t *attr"
299 Initialize a condition variable attributes object with default values.
302 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
304 Unblock all threads currently blocked on the specified condition variable.
307 .Fn pthread_cond_destroy "pthread_cond_t *cond"
309 Destroy a condition variable.
312 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
314 Initialize a condition variable with specified attributes.
317 .Fn pthread_cond_signal "pthread_cond_t *cond"
319 Unblock at least one of the threads blocked on the specified condition variable.
322 .Fn pthread_cond_timedwait "pthread_cond_t *cond" "pthread_mutex_t *mutex" "const struct timespec *abstime"
324 Wait no longer than the specified time for a condition and lock the specified mutex.
327 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
329 Wait for a condition and lock the specified mutex.
331 .Sh READ/WRITE LOCK ROUTINES
335 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
337 Destroy a read/write lock object.
340 .Fn pthread_rwlock_init "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
342 Initialize a read/write lock object.
345 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
347 Lock a read/write lock for reading, blocking until the lock can be
351 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
353 Attempt to lock a read/write lock for reading, without blocking if the
357 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
359 Attempt to lock a read/write lock for writing, without blocking if the
363 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
365 Unlock a read/write lock.
368 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
370 Lock a read/write lock for writing, blocking until the lock can be
374 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
376 Destroy a read/write lock attribute object.
379 .Fn pthread_rwlockattr_getpshared "const pthread_rwlockattr_t *attr" "int *pshared"
381 Retrieve the process shared setting for the read/write lock attribute
385 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
387 Initialize a read/write lock attribute object.
390 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
392 Set the process shared setting for the read/write lock attribute object.
394 .Sh PER-THREAD CONTEXT ROUTINES
398 .Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
400 Create a thread-specific data key.
403 .Fn pthread_key_delete "pthread_key_t key"
405 Delete a thread-specific data key.
408 .Fn pthread_getspecific "pthread_key_t key"
410 Get the thread-specific value for the specified key.
413 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
415 Set the thread-specific value for the specified key.
421 .Fn pthread_cleanup_pop "int execute"
423 Remove the routine at the top of the calling thread's cancellation cleanup
424 stack and optionally invoke it.
427 .Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
429 Push the specified cancellation cleanup handler onto the calling thread's
435 POSIX thread implementation is built in the library
437 which contains both thread-safe libc functions and the thread functions.
438 This library replaces
440 for threaded applications.
444 is built as part of a 'make world'. To disable the build of
446 you must supply the '-DNOLIBC_R' option to
453 specific option exists in
455 to simplify the linking of threaded processes.
457 links a threaded process against
462 .Xr pthread_cleanup_pop 3 ,
463 .Xr pthread_cleanup_push 3 ,
464 .Xr pthread_cond_broadcast 3 ,
465 .Xr pthread_cond_destroy 3 ,
466 .Xr pthread_cond_init 3 ,
467 .Xr pthread_cond_signal 3 ,
468 .Xr pthread_cond_timedwait 3 ,
469 .Xr pthread_cond_wait 3 ,
470 .Xr pthread_condattr_destroy 3 ,
471 .Xr pthread_condattr_init 3 ,
472 .Xr pthread_create 3 ,
473 .Xr pthread_detach 3 ,
474 .Xr pthread_equal 3 ,
476 .Xr pthread_getspecific 3 ,
478 .Xr pthread_key_delete 3 ,
480 .Xr pthread_mutex_destroy 3 ,
481 .Xr pthread_mutex_init 3 ,
482 .Xr pthread_mutex_lock 3 ,
483 .Xr pthread_mutex_trylock 3 ,
484 .Xr pthread_mutex_unlock 3 ,
485 .Xr pthread_mutexattr_destroy 3 ,
486 .Xr pthread_mutexattr_getprioceiling 3 ,
487 .Xr pthread_mutexattr_getprotocol 3 ,
488 .Xr pthread_mutexattr_gettype 3 ,
489 .Xr pthread_mutexattr_init 3 ,
490 .Xr pthread_mutexattr_setprioceiling 3 ,
491 .Xr pthread_mutexattr_setprotocol 3 ,
492 .Xr pthread_mutexattr_settype 3 ,
494 .Xr pthread_rwlockattr_destroy 3 ,
495 .Xr pthread_rwlockattr_getpshared 3 ,
496 .Xr pthread_rwlockattr_init 3 ,
497 .Xr pthread_rwlockattr_setpshared 3 ,
498 .Xr pthread_rwlock_destroy 3 ,
499 .Xr pthread_rwlock_init 3 ,
500 .Xr pthread_rwlock_rdlock 3 ,
501 .Xr pthread_rwlock_unlock 3 ,
502 .Xr pthread_rwlock_wrlock 3 ,
504 .Xr pthread_setcancelstate 3 ,
505 .Xr pthread_setcanceltype 3 ,
506 .Xr pthread_setspecific 3 ,
507 .Xr pthread_testcancel 3
520 The functions in libc_r with the
524 suffix are non-portable extensions to POSIX threads.
526 The functions in libc_r with the
528 prefix are extensions created by The Open Group as part of the