1 .\" Portions Copyright (c) 2001 Apple Computer, Inc. All Rights Reserved.
2 .\" Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
3 .\" All rights reserved.
5 .\" Redistribution and use in source and binary forms, with or without
6 .\" modification, are permitted provided that the following conditions
8 .\" 1. Redistributions of source code must retain the above copyright
9 .\" notice, this list of conditions and the following disclaimer.
10 .\" 2. Redistributions in binary form must reproduce the above copyright
11 .\" notice, this list of conditions and the following disclaimer in the
12 .\" documentation and/or other materials provided with the distribution.
13 .\" 3. All advertising materials mentioning features or use of this software
14 .\" must display the following acknowledgement:
15 .\" This product includes software developed by John Birrell.
16 .\" 4. Neither the name of the author nor the names of any co-contributors
17 .\" may be used to endorse or promote products derived from this software
18 .\" without specific prior written permission.
20 .\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
21 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 .\" $FreeBSD: src/share/man/man3/pthread.3,v 1.12.2.4 2001/08/17 13:08:36 ru Exp $
39 .Nd POSIX thread functions
41 .Fd #include <pthread.h>
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_detach "pthread_t thread"
79 Marks a thread for deletion.
82 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
84 Compares two thread IDs.
87 .Fn pthread_exit "void *value_ptr"
89 Terminates the calling thread.
92 .Fn pthread_join "pthread_t thread" "void **value_ptr"
94 Causes the calling thread to wait for the termination of the specified thread.
97 .Fn pthread_cancel "pthread_t thread"
99 Cancels execution of a thread.
102 .Fn pthread_once "pthread_once_t *once_control" "void (*init_routine)(void)"
104 Calls an initialization routine once.
107 .Fn pthread_self void
109 Returns the thread ID of the calling thread.
111 .Sh ATTRIBUTE OBJECT ROUTINES
115 .Fn pthread_attr_destroy "pthread_attr_t *attr"
117 Destroy a thread attributes object.
120 .Fn pthread_attr_getinheritsched "const pthread_attr_t *attr" "int *inheritsched"
122 Get the inherit scheduling attribute from a thread attributes object.
125 .Fn pthread_attr_getschedparam "const pthread_attr_t *attr" "struct sched_param *param"
127 Get the scheduling parameter attribute from a thread attributes object.
130 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
132 Get the scheduling policy attribute from a thread attributes object.
135 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
137 Get the contention scope attribute from a thread attributes object.
140 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
142 Get the stack size attribute from a thread attributes object.
145 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
147 Get the stack address attribute from a thread attributes object.
150 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
152 Get the detach state attribute from a thread attributes object.
155 .Fn pthread_attr_init "pthread_attr_t *attr"
157 Initialize a thread attributes object with default values.
160 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
162 Set the inherit scheduling attribute in a thread attributes object.
165 .Fn pthread_attr_setschedparam "pthread_attr_t *attr" "const struct sched_param *param"
167 Set the scheduling parameter attribute in a thread attributes object.
170 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
172 Set the scheduling policy attribute in a thread attributes object.
175 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
177 Set the contention scope attribute in a thread attributes object.
180 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
182 Set the stack size attribute in a thread attributes object.
185 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
187 Set the stack address attribute in a thread attributes object.
190 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
192 Set the detach state in a thread attributes object.
198 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
200 Destroy a mutex attributes object.
203 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
205 Initialize a mutex attributes object with default values.
208 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
213 .Fn pthread_mutex_init "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
215 Initialize a mutex with specified attributes.
218 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
220 Lock a mutex and block until it becomes available.
223 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
225 Try to lock a mutex, but don't block if the mutex is locked by another thread,
226 including the current thread.
229 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
233 .Sh CONDITION VARIABLE ROUTINES
237 .Fn pthread_condattr_init "pthread_condattr_t *attr"
239 Initialize a condition variable attributes object with default values.
242 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
244 Destroy a condition variable attributes object.
247 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
249 Unblock all threads currently blocked on the specified condition variable.
252 .Fn pthread_cond_destroy "pthread_cond_t *cond"
254 Destroy a condition variable.
257 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
259 Initialize a condition variable with specified attributes.
262 .Fn pthread_cond_signal "pthread_cond_t *cond"
264 Unblock at least one of the threads blocked on the specified condition variable.
267 .Fn pthread_cond_timedwait "pthread_cond_t *cond" "pthread_mutex_t *mutex" "const struct timespec *abstime"
269 Wait no longer than the specified time for a condition and lock the specified mutex.
272 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
274 Wait for a condition and lock the specified mutex.
276 .Sh READ/WRITE LOCK ROUTINES
280 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
282 Destroy a read/write lock object.
285 .Fn pthread_rwlock_init "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
287 Initialize a read/write lock object.
290 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
292 Lock a read/write lock for reading, blocking until the lock can be
296 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
298 Attempt to lock a read/write lock for reading, without blocking if the
302 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
304 Attempt to lock a read/write lock for writing, without blocking if the
308 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
310 Unlock a read/write lock.
313 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
315 Lock a read/write lock for writing, blocking until the lock can be
319 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
321 Destroy a read/write lock attribute object.
324 .Fn pthread_rwlockattr_getpshared "const pthread_rwlockattr_t *attr" "int *pshared"
326 Retrieve the process shared setting for the read/write lock attribute
330 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
332 Initialize a read/write lock attribute object.
335 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
337 Set the process shared setting for the read/write lock attribute object.
339 .Sh PER-THREAD CONTEXT ROUTINES
343 .Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
345 Create a thread-specific data key.
348 .Fn pthread_key_delete "pthread_key_t key"
350 Delete a thread-specific data key.
353 .Fn pthread_getspecific "pthread_key_t key"
355 Get the thread-specific value for the specified key.
358 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
360 Set the thread-specific value for the specified key.
366 .Fn pthread_cleanup_pop "int execute"
368 Remove the routine at the top of the calling thread's cancellation cleanup
369 stack and optionally invoke it.
372 .Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
374 Push the specified cancellation cleanup handler onto the calling thread's
378 The default system libraries include
380 funcitons. No additional libraries or CFLAGS are necessary to use this API.
382 .Xr pthread_cleanup_pop 3 ,
383 .Xr pthread_cleanup_push 3 ,
384 .Xr pthread_cond_broadcast 3 ,
385 .Xr pthread_cond_destroy 3 ,
386 .Xr pthread_cond_init 3 ,
387 .Xr pthread_cond_signal 3 ,
388 .Xr pthread_cond_timedwait 3 ,
389 .Xr pthread_cond_wait 3 ,
390 .Xr pthread_create 3 ,
391 .Xr pthread_detach 3 ,
392 .Xr pthread_equal 3 ,
394 .Xr pthread_getspecific 3 ,
396 .Xr pthread_key_delete 3 ,
397 .Xr pthread_mutex_destroy 3 ,
398 .Xr pthread_mutex_init 3 ,
399 .Xr pthread_mutex_lock 3 ,
400 .Xr pthread_mutex_trylock 3 ,
401 .Xr pthread_mutex_unlock 3 ,
403 .Xr pthread_rwlockattr_destroy 3 ,
404 .Xr pthread_rwlockattr_getpshared 3 ,
405 .Xr pthread_rwlockattr_init 3 ,
406 .Xr pthread_rwlockattr_setpshared 3 ,
407 .Xr pthread_rwlock_destroy 3 ,
408 .Xr pthread_rwlock_init 3 ,
409 .Xr pthread_rwlock_rdlock 3 ,
410 .Xr pthread_rwlock_unlock 3 ,
411 .Xr pthread_rwlock_wrlock 3 ,
413 .Xr pthread_setspecific 3
426 The functions in libc with the
430 suffix are non-portable extensions to POSIX threads.
432 The functions in libc with the
434 prefix are extensions created by The Open Group as part of the