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.5 2007/09/14 19:47:03 swildner Exp $
34 .Dd September 10, 1998
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
54 .Bl -bullet -offset indent
58 Attribute Object Routines
62 Condition Variable Routines
64 Read/Write Lock Routines
66 Per-Thread Context Routines
74 .Fn pthread_create "pthread_t *thread" "const pthread_attr_t *attr" "void *(*start_routine)(void *)" "void *arg"
76 Creates a new thread of execution.
79 .Fn pthread_cancel "pthread_t thread"
81 Cancels execution of a thread.
84 .Fn pthread_detach "pthread_t thread"
86 Marks a thread for deletion.
89 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
91 Compares two thread IDs.
94 .Fn pthread_exit "void *value_ptr"
96 Terminates the calling thread.
99 .Fn pthread_join "pthread_t thread" "void **value_ptr"
101 Causes the calling thread to wait for the termination of the specified thread.
104 .Fn pthread_kill "pthread_t thread" "int sig"
106 Delivers a signal to a specified thread.
109 .Fn pthread_once "pthread_once_t *once_control" "void (*init_routine)(void)"
111 Calls an initialization routine once.
114 .Fn pthread_self void
116 Returns the thread ID of the calling thread.
119 .Fn pthread_setcancelstate "int state" "int *oldstate"
121 Sets the current thread's cancelability state.
124 .Fn pthread_setcanceltype "int type" "int *oldtype"
126 Sets the current thread's cancelability type.
129 .Fn pthread_testcancel void
131 Creates a cancellation point in the calling thread.
134 .Fn pthread_yield void
136 Allows the scheduler to run another thread instead of the current one.
138 .Sh ATTRIBUTE OBJECT ROUTINES
142 .Fn pthread_attr_destroy "pthread_attr_t *attr"
144 Destroy a thread attributes object.
147 .Fn pthread_attr_getinheritsched "const pthread_attr_t *attr" "int *inheritsched"
149 Get the inherit scheduling attribute from a thread attributes object.
152 .Fn pthread_attr_getschedparam "const pthread_attr_t *attr" "struct sched_param *param"
154 Get the scheduling parameter attribute from a thread attributes object.
157 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
159 Get the scheduling policy attribute from a thread attributes object.
162 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
164 Get the contention scope attribute from a thread attributes object.
167 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
169 Get the stack size attribute from a thread attributes object.
172 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
174 Get the stack address attribute from a thread attributes object.
177 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
179 Get the detach state attribute from a thread attributes object.
182 .Fn pthread_attr_init "pthread_attr_t *attr"
184 Initialize a thread attributes object with default values.
187 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
189 Set the inherit scheduling attribute in a thread attributes object.
192 .Fn pthread_attr_setschedparam "pthread_attr_t *attr" "const struct sched_param *param"
194 Set the scheduling parameter attribute in a thread attributes object.
197 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
199 Set the scheduling policy attribute in a thread attributes object.
202 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
204 Set the contention scope attribute in a thread attributes object.
207 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
209 Set the stack size attribute in a thread attributes object.
212 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
214 Set the stack address attribute in a thread attributes object.
217 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
219 Set the detach state in a thread attributes object.
225 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
227 Destroy a mutex attributes object.
230 .Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *ceiling"
232 Obtain priority ceiling attribute of mutex attribute object.
235 .Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
237 Obtain protocol attribute of mutex attribute object.
240 .Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
242 Obtain the mutex type attribute in the specified mutex attributes object.
245 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
247 Initialize a mutex attributes object with default values.
250 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int ceiling"
252 Set priority ceiling attribute of mutex attribute object.
255 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
257 Set protocol attribute of mutex attribute object.
260 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
262 Set the mutex type attribute that is used when a mutex is created.
265 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
270 .Fn pthread_mutex_init "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
272 Initialize a mutex with specified attributes.
275 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
277 Lock a mutex and block until it becomes available.
280 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
282 Try to lock a mutex, but don't block if the mutex is locked by another thread,
283 including the current thread.
286 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
290 .Sh CONDITION VARIABLE ROUTINES
294 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
296 Destroy a condition variable attributes object.
299 .Fn pthread_condattr_init "pthread_condattr_t *attr"
301 Initialize a condition variable attributes object with default values.
304 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
306 Unblock all threads currently blocked on the specified condition variable.
309 .Fn pthread_cond_destroy "pthread_cond_t *cond"
311 Destroy a condition variable.
314 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
316 Initialize a condition variable with specified attributes.
319 .Fn pthread_cond_signal "pthread_cond_t *cond"
321 Unblock at least one of the threads blocked on the specified condition variable.
324 .Fn pthread_cond_timedwait "pthread_cond_t *cond" "pthread_mutex_t *mutex" "const struct timespec *abstime"
326 Wait no longer than the specified time for a condition and lock the specified mutex.
329 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
331 Wait for a condition and lock the specified mutex.
333 .Sh READ/WRITE LOCK ROUTINES
337 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
339 Destroy a read/write lock object.
342 .Fn pthread_rwlock_init "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
344 Initialize a read/write lock object.
347 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
349 Lock a read/write lock for reading, blocking until the lock can be
353 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
355 Attempt to lock a read/write lock for reading, without blocking if the
359 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
361 Attempt to lock a read/write lock for writing, without blocking if the
365 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
367 Unlock a read/write lock.
370 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
372 Lock a read/write lock for writing, blocking until the lock can be
376 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
378 Destroy a read/write lock attribute object.
381 .Fn pthread_rwlockattr_getpshared "const pthread_rwlockattr_t *attr" "int *pshared"
383 Retrieve the process shared setting for the read/write lock attribute
387 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
389 Initialize a read/write lock attribute object.
392 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
394 Set the process shared setting for the read/write lock attribute object.
396 .Sh PER-THREAD CONTEXT ROUTINES
400 .Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
402 Create a thread-specific data key.
405 .Fn pthread_key_delete "pthread_key_t key"
407 Delete a thread-specific data key.
410 .Fn pthread_getspecific "pthread_key_t key"
412 Get the thread-specific value for the specified key.
415 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
417 Set the thread-specific value for the specified key.
423 .Fn pthread_cleanup_pop "int execute"
425 Remove the routine at the top of the calling thread's cancellation cleanup
426 stack and optionally invoke it.
429 .Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
431 Push the specified cancellation cleanup handler onto the calling thread's
437 POSIX thread implementation is built in the library
439 which contains both thread-safe libc functions and the thread functions.
440 This library replaces
442 for threaded applications.
446 is built as part of a 'make world'. To disable the build of
448 you must supply the '-DNOLIBC_R' option to
455 specific option exists in
457 to simplify the linking of threaded processes.
459 links a threaded process against
464 .Xr pthread_cleanup_pop 3 ,
465 .Xr pthread_cleanup_push 3 ,
466 .Xr pthread_cond_broadcast 3 ,
467 .Xr pthread_cond_destroy 3 ,
468 .Xr pthread_cond_init 3 ,
469 .Xr pthread_cond_signal 3 ,
470 .Xr pthread_cond_timedwait 3 ,
471 .Xr pthread_cond_wait 3 ,
472 .Xr pthread_condattr_destroy 3 ,
473 .Xr pthread_condattr_init 3 ,
474 .Xr pthread_create 3 ,
475 .Xr pthread_detach 3 ,
476 .Xr pthread_equal 3 ,
478 .Xr pthread_getspecific 3 ,
480 .Xr pthread_key_delete 3 ,
482 .Xr pthread_mutex_destroy 3 ,
483 .Xr pthread_mutex_init 3 ,
484 .Xr pthread_mutex_lock 3 ,
485 .Xr pthread_mutex_trylock 3 ,
486 .Xr pthread_mutex_unlock 3 ,
487 .Xr pthread_mutexattr_destroy 3 ,
488 .Xr pthread_mutexattr_getprioceiling 3 ,
489 .Xr pthread_mutexattr_getprotocol 3 ,
490 .Xr pthread_mutexattr_gettype 3 ,
491 .Xr pthread_mutexattr_init 3 ,
492 .Xr pthread_mutexattr_setprioceiling 3 ,
493 .Xr pthread_mutexattr_setprotocol 3 ,
494 .Xr pthread_mutexattr_settype 3 ,
496 .Xr pthread_rwlockattr_destroy 3 ,
497 .Xr pthread_rwlockattr_getpshared 3 ,
498 .Xr pthread_rwlockattr_init 3 ,
499 .Xr pthread_rwlockattr_setpshared 3 ,
500 .Xr pthread_rwlock_destroy 3 ,
501 .Xr pthread_rwlock_init 3 ,
502 .Xr pthread_rwlock_rdlock 3 ,
503 .Xr pthread_rwlock_unlock 3 ,
504 .Xr pthread_rwlock_wrlock 3 ,
506 .Xr pthread_setcancelstate 3 ,
507 .Xr pthread_setcanceltype 3 ,
508 .Xr pthread_setspecific 3 ,
509 .Xr pthread_testcancel 3
522 The functions in libc_r with the
526 suffix are non-portable extensions to POSIX threads.
528 The functions in libc_r with the
530 prefix are extensions created by The Open Group as part of the