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
38 .Nd POSIX thread functions
44 POSIX threads are a set of functions that support applications with
45 requirements for multiple flows of control, called
48 Multithreading is used to improve the performance of a
51 The POSIX thread functions are summarized in this section in the following
54 .Bl -bullet -offset indent -compact
58 Attribute Object Routines
62 Condition Variable Routines
64 Read/Write Lock Routines
66 Per-Thread Context Routines
71 .Bl -tag -width indent
75 .Fa "pthread_t *thread" "const pthread_attr_t *attr"
76 .Fa "void *\*[lp]*start_routine\*[rp]\*[lp]void *\*[rp]" "void *arg"
79 Creates a new thread of execution.
82 .Fn pthread_cancel "pthread_t thread"
84 Cancels execution of a thread.
87 .Fn pthread_detach "pthread_t thread"
89 Marks a thread for deletion.
92 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
94 Compares two thread IDs.
97 .Fn pthread_exit "void *value_ptr"
99 Terminates the calling thread.
102 .Fn pthread_join "pthread_t thread" "void **value_ptr"
104 Causes the calling thread to wait for the termination of the specified thread.
107 .Fn pthread_kill "pthread_t thread" "int sig"
109 Delivers a signal to a specified thread.
112 .Fn pthread_once "pthread_once_t *once_control" "void \*[lp]*init_routine\*[rp]\*[lp]void\*[rp]"
114 Calls an initialization routine once.
117 .Fn pthread_self void
119 Returns the thread ID of the calling thread.
122 .Fn pthread_setcancelstate "int state" "int *oldstate"
124 Sets the current thread's cancelability state.
127 .Fn pthread_setcanceltype "int type" "int *oldtype"
129 Sets the current thread's cancelability type.
132 .Fn pthread_testcancel void
134 Creates a cancellation point in the calling thread.
137 .Fn pthread_yield void
139 Allows the scheduler to run another thread instead of the current one.
141 .Ss Attribute Object Routines
142 .Bl -tag -width indent
145 .Fn pthread_attr_destroy "pthread_attr_t *attr"
147 Destroy a thread attributes object.
150 .Fo pthread_attr_getinheritsched
151 .Fa "const pthread_attr_t *attr" "int *inheritsched"
154 Get the inherit scheduling attribute from a thread attributes object.
157 .Fo pthread_attr_getschedparam
158 .Fa "const pthread_attr_t *attr" "struct sched_param *param"
161 Get the scheduling parameter attribute from a thread attributes object.
164 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
166 Get the scheduling policy attribute from a thread attributes object.
169 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
171 Get the contention scope attribute from a thread attributes object.
174 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
176 Get the stack size attribute from a thread attributes object.
179 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
181 Get the stack address attribute from a thread attributes object.
184 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
186 Get the detach state attribute from a thread attributes object.
189 .Fn pthread_attr_init "pthread_attr_t *attr"
191 Initialize a thread attributes object with default values.
194 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
196 Set the inherit scheduling attribute in a thread attributes object.
199 .Fo pthread_attr_setschedparam
200 .Fa "pthread_attr_t *attr" "const struct sched_param *param"
203 Set the scheduling parameter attribute in a thread attributes object.
206 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
208 Set the scheduling policy attribute in a thread attributes object.
211 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
213 Set the contention scope attribute in a thread attributes object.
216 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
218 Set the stack size attribute in a thread attributes object.
221 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
223 Set the stack address attribute in a thread attributes object.
226 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
228 Set the detach state in a thread attributes object.
231 .Bl -tag -width indent
234 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
236 Destroy a mutex attributes object.
239 .Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *ceiling"
241 Obtain priority ceiling attribute of mutex attribute object.
244 .Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
246 Obtain protocol attribute of mutex attribute object.
249 .Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
251 Obtain the mutex type attribute in the specified mutex attributes object.
254 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
256 Initialize a mutex attributes object with default values.
259 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int ceiling"
261 Set priority ceiling attribute of mutex attribute object.
264 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
266 Set protocol attribute of mutex attribute object.
269 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
271 Set the mutex type attribute that is used when a mutex is created.
274 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
279 .Fo pthread_mutex_init
280 .Fa "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
283 Initialize a mutex with specified attributes.
286 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
288 Lock a mutex and block until it becomes available.
291 .Fo pthread_mutex_timedlock
292 .Fa "pthread_mutex_t *mutex" "const struct timespec *abstime"
295 Lock a mutex and block until it becomes available or until the timeout expires.
298 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
300 Try to lock a mutex, but do not block if the mutex is locked by another thread,
301 including the current thread.
304 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
308 .Ss Condition Variable Routines
309 .Bl -tag -width indent
312 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
314 Destroy a condition variable attributes object.
317 .Fn pthread_condattr_init "pthread_condattr_t *attr"
319 Initialize a condition variable attributes object with default values.
322 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
324 Unblock all threads currently blocked on the specified condition variable.
327 .Fn pthread_cond_destroy "pthread_cond_t *cond"
329 Destroy a condition variable.
332 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
334 Initialize a condition variable with specified attributes.
337 .Fn pthread_cond_signal "pthread_cond_t *cond"
339 Unblock at least one of the threads blocked on the specified condition variable.
342 .Fo pthread_cond_timedwait
343 .Fa "pthread_cond_t *cond" "pthread_mutex_t *mutex"
344 .Fa "const struct timespec *abstime"
347 Wait no longer than the specified time for a condition
348 and lock the specified mutex.
351 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
353 Wait for a condition and lock the specified mutex.
355 .Ss Read/Write Lock Routines
356 .Bl -tag -width indent
359 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
361 Destroy a read/write lock object.
364 .Fo pthread_rwlock_init
365 .Fa "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
368 Initialize a read/write lock object.
371 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
373 Lock a read/write lock for reading, blocking until the lock can be
377 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
379 Attempt to lock a read/write lock for reading, without blocking if the
383 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
385 Attempt to lock a read/write lock for writing, without blocking if the
389 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
391 Unlock a read/write lock.
394 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
396 Lock a read/write lock for writing, blocking until the lock can be
400 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
402 Destroy a read/write lock attribute object.
405 .Fo pthread_rwlockattr_getpshared
406 .Fa "const pthread_rwlockattr_t *attr" "int *pshared"
409 Retrieve the process shared setting for the read/write lock attribute
413 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
415 Initialize a read/write lock attribute object.
418 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
420 Set the process shared setting for the read/write lock attribute object.
422 .Ss Per-Thread Context Routines
423 .Bl -tag -width indent
426 .Fn pthread_key_create "pthread_key_t *key" "void \*[lp]*routine\*[rp]\*[lp]void *\*[rp]"
428 Create a thread-specific data key.
431 .Fn pthread_key_delete "pthread_key_t key"
433 Delete a thread-specific data key.
436 .Fn pthread_getspecific "pthread_key_t key"
438 Get the thread-specific value for the specified key.
441 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
443 Set the thread-specific value for the specified key.
446 .Bl -tag -width indent
450 .Fa "void \*[lp]*prepare\*[rp]\*[lp]void\*[rp]"
451 .Fa "void \*[lp]*parent\*[rp]\*[lp]void\*[rp]"
452 .Fa "void \*[lp]*child\*[rp]\*[lp]void\*[rp]"
455 Register fork handlers
458 .Fn pthread_cleanup_pop "int execute"
460 Remove the routine at the top of the calling thread's cancellation cleanup
461 stack and optionally invoke it.
464 .Fn pthread_cleanup_push "void \*[lp]*routine\*[rp]\*[lp]void *\*[rp]" "void *routine_arg"
466 Push the specified cancellation cleanup handler onto the calling thread's
469 .Sh IMPLEMENTATION NOTES
472 POSIX thread implementation is built in two libraries,
476 They contain both thread-safe versions of
478 functions and the thread functions.
479 Threaded applications are linked with one of these libraries.
481 .Xr pthread_atfork 3 ,
482 .Xr pthread_cancel 3 ,
483 .Xr pthread_cleanup_pop 3 ,
484 .Xr pthread_cleanup_push 3 ,
485 .Xr pthread_condattr_destroy 3 ,
486 .Xr pthread_condattr_init 3 ,
487 .Xr pthread_cond_broadcast 3 ,
488 .Xr pthread_cond_destroy 3 ,
489 .Xr pthread_cond_init 3 ,
490 .Xr pthread_cond_signal 3 ,
491 .Xr pthread_cond_timedwait 3 ,
492 .Xr pthread_cond_wait 3 ,
493 .Xr pthread_create 3 ,
494 .Xr pthread_detach 3 ,
495 .Xr pthread_equal 3 ,
497 .Xr pthread_getspecific 3 ,
499 .Xr pthread_key_delete 3 ,
501 .Xr pthread_mutexattr_destroy 3 ,
502 .Xr pthread_mutexattr_getprioceiling 3 ,
503 .Xr pthread_mutexattr_getprotocol 3 ,
504 .Xr pthread_mutexattr_gettype 3 ,
505 .Xr pthread_mutexattr_init 3 ,
506 .Xr pthread_mutexattr_setprioceiling 3 ,
507 .Xr pthread_mutexattr_setprotocol 3 ,
508 .Xr pthread_mutexattr_settype 3 ,
509 .Xr pthread_mutex_destroy 3 ,
510 .Xr pthread_mutex_init 3 ,
511 .Xr pthread_mutex_lock 3 ,
512 .Xr pthread_mutex_trylock 3 ,
513 .Xr pthread_mutex_unlock 3 ,
515 .Xr pthread_rwlockattr_destroy 3 ,
516 .Xr pthread_rwlockattr_getpshared 3 ,
517 .Xr pthread_rwlockattr_init 3 ,
518 .Xr pthread_rwlockattr_setpshared 3 ,
519 .Xr pthread_rwlock_destroy 3 ,
520 .Xr pthread_rwlock_init 3 ,
521 .Xr pthread_rwlock_rdlock 3 ,
522 .Xr pthread_rwlock_unlock 3 ,
523 .Xr pthread_rwlock_wrlock 3 ,
525 .Xr pthread_setcancelstate 3 ,
526 .Xr pthread_setcanceltype 3 ,
527 .Xr pthread_setspecific 3 ,
528 .Xr pthread_testcancel 3
530 The functions with the
539 The functions with the
543 suffix are non-portable extensions to POSIX threads.
545 The functions with the
547 prefix are extensions created by The Open Group as part of the