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 $
33 .Dd September 10, 1998
38 .Nd POSIX thread functions
42 POSIX threads are a set of functions that support applications with
43 requirements for multiple flows of control, called
46 Multithreading is used to improve the performance of a
49 The POSIX thread functions are summarized in this section in the following
51 .Bl -bullet -offset indent
55 Attribute Object Routines
59 Condition Variable Routines
61 Read/Write Lock Routines
63 Per-Thread Context Routines
71 .Fn pthread_create "pthread_t *thread" "const pthread_attr_t *attr" "void *(*start_routine)(void *)" "void *arg"
73 Creates a new thread of execution.
76 .Fn pthread_cancel "pthread_t thread"
78 Cancels execution of a thread.
81 .Fn pthread_detach "pthread_t thread"
83 Marks a thread for deletion.
86 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
88 Compares two thread IDs.
91 .Fn pthread_exit "void *value_ptr"
93 Terminates the calling thread.
96 .Fn pthread_join "pthread_t thread" "void **value_ptr"
98 Causes the calling thread to wait for the termination of the specified thread.
101 .Fn pthread_kill "pthread_t thread" "int sig"
103 Delivers a signal to a specified thread.
106 .Fn pthread_once "pthread_once_t *once_control" "void (*init_routine)(void)"
108 Calls an initialization routine once.
111 .Fn pthread_self void
113 Returns the thread ID of the calling thread.
116 .Fn pthread_setcancelstate "int state" "int *oldstate"
118 Sets the current thread's cancelability state.
121 .Fn pthread_setcanceltype "int type" "int *oldtype"
123 Sets the current thread's cancelability type.
126 .Fn pthread_testcancel void
128 Creates a cancellation point in the calling thread.
131 .Fn pthread_yield void
133 Allows the scheduler to run another thread instead of the current one.
135 .Sh ATTRIBUTE OBJECT ROUTINES
139 .Fn pthread_attr_destroy "pthread_attr_t *attr"
141 Destroy a thread attributes object.
144 .Fn pthread_attr_getinheritsched "const pthread_attr_t *attr" "int *inheritsched"
146 Get the inherit scheduling attribute from a thread attributes object.
149 .Fn pthread_attr_getschedparam "const pthread_attr_t *attr" "struct sched_param *param"
151 Get the scheduling parameter attribute from a thread attributes object.
154 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
156 Get the scheduling policy attribute from a thread attributes object.
159 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
161 Get the contention scope attribute from a thread attributes object.
164 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
166 Get the stack size attribute from a thread attributes object.
169 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
171 Get the stack address attribute from a thread attributes object.
174 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
176 Get the detach state attribute from a thread attributes object.
179 .Fn pthread_attr_init "pthread_attr_t *attr"
181 Initialize a thread attributes object with default values.
184 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
186 Set the inherit scheduling attribute in a thread attributes object.
189 .Fn pthread_attr_setschedparam "pthread_attr_t *attr" "const struct sched_param *param"
191 Set the scheduling parameter attribute in a thread attributes object.
194 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
196 Set the scheduling policy attribute in a thread attributes object.
199 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
201 Set the contention scope attribute in a thread attributes object.
204 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
206 Set the stack size attribute in a thread attributes object.
209 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
211 Set the stack address attribute in a thread attributes object.
214 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
216 Set the detach state in a thread attributes object.
222 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
224 Destroy a mutex attributes object.
227 .Fn pthread_mutexattr_getprioceiling "pthread_mutexattr_t *attr" "int *ceiling"
229 Obtain priority ceiling attribute of mutex attribute object.
232 .Fn pthread_mutexattr_getprotocol "pthread_mutexattr_t *attr" "int *protocol"
234 Obtain protocol attribute of mutex attribute object.
237 .Fn pthread_mutexattr_gettype "pthread_mutexattr_t *attr" "int *type"
239 Obtain the mutex type attribute in the specified mutex attributes object.
242 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
244 Initialize a mutex attributes object with default values.
247 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int ceiling"
249 Set priority ceiling attribute of mutex attribute object.
252 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
254 Set protocol attribute of mutex attribute object.
257 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
259 Set the mutex type attribute that is used when a mutex is created.
262 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
267 .Fn pthread_mutex_init "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
269 Initialize a mutex with specified attributes.
272 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
274 Lock a mutex and block until it becomes available.
277 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
279 Try to lock a mutex, but don't block if the mutex is locked by another thread,
280 including the current thread.
283 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
287 .Sh CONDITION VARIABLE ROUTINES
291 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
293 Destroy a condition variable attributes object.
296 .Fn pthread_condattr_init "pthread_condattr_t *attr"
298 Initialize a condition variable attributes object with default values.
301 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
303 Unblock all threads currently blocked on the specified condition variable.
306 .Fn pthread_cond_destroy "pthread_cond_t *cond"
308 Destroy a condition variable.
311 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
313 Initialize a condition variable with specified attributes.
316 .Fn pthread_cond_signal "pthread_cond_t *cond"
318 Unblock at least one of the threads blocked on the specified condition variable.
321 .Fn pthread_cond_timedwait "pthread_cond_t *cond" "pthread_mutex_t *mutex" "const struct timespec *abstime"
323 Wait no longer than the specified time for a condition and lock the specified mutex.
326 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
328 Wait for a condition and lock the specified mutex.
330 .Sh READ/WRITE LOCK ROUTINES
334 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
336 Destroy a read/write lock object.
339 .Fn pthread_rwlock_init "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
341 Initialize a read/write lock object.
344 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
346 Lock a read/write lock for reading, blocking until the lock can be
350 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
352 Attempt to lock a read/write lock for reading, without blocking if the
356 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
358 Attempt to lock a read/write lock for writing, without blocking if the
362 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
364 Unlock a read/write lock.
367 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
369 Lock a read/write lock for writing, blocking until the lock can be
373 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
375 Destroy a read/write lock attribute object.
378 .Fn pthread_rwlockattr_getpshared "const pthread_rwlockattr_t *attr" "int *pshared"
380 Retrieve the process shared setting for the read/write lock attribute
384 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
386 Initialize a read/write lock attribute object.
389 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
391 Set the process shared setting for the read/write lock attribute object.
393 .Sh PER-THREAD CONTEXT ROUTINES
397 .Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
399 Create a thread-specific data key.
402 .Fn pthread_key_delete "pthread_key_t key"
404 Delete a thread-specific data key.
407 .Fn pthread_getspecific "pthread_key_t key"
409 Get the thread-specific value for the specified key.
412 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
414 Set the thread-specific value for the specified key.
420 .Fn pthread_cleanup_pop "int execute"
422 Remove the routine at the top of the calling thread's cancellation cleanup
423 stack and optionally invoke it.
426 .Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
428 Push the specified cancellation cleanup handler onto the calling thread's
434 POSIX thread implementation is built in the library
436 which contains both thread-safe libc functions and the thread functions.
437 This library replaces
439 for threaded applications.
443 is built as part of a 'make world'. To disable the build of
445 you must supply the '-DNOLIBC_R' option to
450 specific option has been added to gcc to make linking
451 threaded processes simple.
453 links a threaded process against
458 .Xr pthread_cleanup_pop 3 ,
459 .Xr pthread_cleanup_push 3 ,
460 .Xr pthread_cond_broadcast 3 ,
461 .Xr pthread_cond_destroy 3 ,
462 .Xr pthread_cond_init 3 ,
463 .Xr pthread_cond_signal 3 ,
464 .Xr pthread_cond_timedwait 3 ,
465 .Xr pthread_cond_wait 3 ,
466 .Xr pthread_condattr_destroy 3 ,
467 .Xr pthread_condattr_init 3 ,
468 .Xr pthread_create 3 ,
469 .Xr pthread_detach 3 ,
470 .Xr pthread_equal 3 ,
472 .Xr pthread_getspecific 3 ,
474 .Xr pthread_key_delete 3 ,
476 .Xr pthread_mutex_destroy 3 ,
477 .Xr pthread_mutex_init 3 ,
478 .Xr pthread_mutex_lock 3 ,
479 .Xr pthread_mutex_trylock 3 ,
480 .Xr pthread_mutex_unlock 3 ,
481 .Xr pthread_mutexattr_destroy 3 ,
482 .Xr pthread_mutexattr_getprioceiling 3 ,
483 .Xr pthread_mutexattr_getprotocol 3 ,
484 .Xr pthread_mutexattr_gettype 3 ,
485 .Xr pthread_mutexattr_init 3 ,
486 .Xr pthread_mutexattr_setprioceiling 3 ,
487 .Xr pthread_mutexattr_setprotocol 3 ,
488 .Xr pthread_mutexattr_settype 3 ,
490 .Xr pthread_rwlockattr_destroy 3 ,
491 .Xr pthread_rwlockattr_getpshared 3 ,
492 .Xr pthread_rwlockattr_init 3 ,
493 .Xr pthread_rwlockattr_setpshared 3 ,
494 .Xr pthread_rwlock_destroy 3 ,
495 .Xr pthread_rwlock_init 3 ,
496 .Xr pthread_rwlock_rdlock 3 ,
497 .Xr pthread_rwlock_unlock 3 ,
498 .Xr pthread_rwlock_wrlock 3 ,
500 .Xr pthread_setcancelstate 3 ,
501 .Xr pthread_setcanceltype 3 ,
502 .Xr pthread_setspecific 3 ,
503 .Xr pthread_testcancel 3
516 The functions in libc_r with the
520 suffix are non-portable extensions to POSIX threads.
522 The functions in libc_r with the
524 prefix are extensions created by The Open Group as part of the