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.
112 .Fn pthread_atfork "void (*prepare)(void)" "void (*parent)(void)" "void (*child)(void)"
114 Registers handlers to be called before and after
117 .Sh ATTRIBUTE OBJECT ROUTINES
121 .Fn pthread_attr_destroy "pthread_attr_t *attr"
123 Destroy a thread attributes object.
126 .Fn pthread_attr_getinheritsched "const pthread_attr_t *attr" "int *inheritsched"
128 Get the inherit scheduling attribute from a thread attributes object.
131 .Fn pthread_attr_getschedparam "const pthread_attr_t *attr" "struct sched_param *param"
133 Get the scheduling parameter attribute from a thread attributes object.
136 .Fn pthread_attr_getschedpolicy "const pthread_attr_t *attr" "int *policy"
138 Get the scheduling policy attribute from a thread attributes object.
141 .Fn pthread_attr_getscope "const pthread_attr_t *attr" "int *contentionscope"
143 Get the contention scope attribute from a thread attributes object.
146 .Fn pthread_attr_getstacksize "const pthread_attr_t *attr" "size_t *stacksize"
148 Get the stack size attribute from a thread attributes object.
151 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
153 Get the stack address attribute from a thread attributes object.
156 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
158 Get the detach state attribute from a thread attributes object.
161 .Fn pthread_attr_init "pthread_attr_t *attr"
163 Initialize a thread attributes object with default values.
166 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
168 Set the inherit scheduling attribute in a thread attributes object.
171 .Fn pthread_attr_setschedparam "pthread_attr_t *attr" "const struct sched_param *param"
173 Set the scheduling parameter attribute in a thread attributes object.
176 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
178 Set the scheduling policy attribute in a thread attributes object.
181 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
183 Set the contention scope attribute in a thread attributes object.
186 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
188 Set the stack size attribute in a thread attributes object.
191 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
193 Set the stack address attribute in a thread attributes object.
196 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
198 Set the detach state in a thread attributes object.
204 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
206 Destroy a mutex attributes object.
209 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
211 Initialize a mutex attributes object with default values.
214 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
219 .Fn pthread_mutex_init "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
221 Initialize a mutex with specified attributes.
224 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
226 Lock a mutex and block until it becomes available.
229 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
231 Try to lock a mutex, but don't block if the mutex is locked by another thread,
232 including the current thread.
235 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
239 .Sh CONDITION VARIABLE ROUTINES
243 .Fn pthread_condattr_init "pthread_condattr_t *attr"
245 Initialize a condition variable attributes object with default values.
248 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
250 Destroy a condition variable attributes object.
253 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
255 Unblock all threads currently blocked on the specified condition variable.
258 .Fn pthread_cond_destroy "pthread_cond_t *cond"
260 Destroy a condition variable.
263 .Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
265 Initialize a condition variable with specified attributes.
268 .Fn pthread_cond_signal "pthread_cond_t *cond"
270 Unblock at least one of the threads blocked on the specified condition variable.
273 .Fn pthread_cond_timedwait "pthread_cond_t *cond" "pthread_mutex_t *mutex" "const struct timespec *abstime"
275 Wait no longer than the specified time for a condition and lock the specified mutex.
278 .Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
280 Wait for a condition and lock the specified mutex.
282 .Sh READ/WRITE LOCK ROUTINES
286 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
288 Destroy a read/write lock object.
291 .Fn pthread_rwlock_init "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
293 Initialize a read/write lock object.
296 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
298 Lock a read/write lock for reading, blocking until the lock can be
302 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
304 Attempt to lock a read/write lock for reading, without blocking if the
308 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
310 Attempt to lock a read/write lock for writing, without blocking if the
314 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
316 Unlock a read/write lock.
319 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
321 Lock a read/write lock for writing, blocking until the lock can be
325 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
327 Destroy a read/write lock attribute object.
330 .Fn pthread_rwlockattr_getpshared "const pthread_rwlockattr_t *attr" "int *pshared"
332 Retrieve the process shared setting for the read/write lock attribute
336 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
338 Initialize a read/write lock attribute object.
341 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
343 Set the process shared setting for the read/write lock attribute object.
345 .Sh PER-THREAD CONTEXT ROUTINES
349 .Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
351 Create a thread-specific data key.
354 .Fn pthread_key_delete "pthread_key_t key"
356 Delete a thread-specific data key.
359 .Fn pthread_getspecific "pthread_key_t key"
361 Get the thread-specific value for the specified key.
364 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
366 Set the thread-specific value for the specified key.
372 .Fn pthread_cleanup_pop "int execute"
374 Remove the routine at the top of the calling thread's cancellation cleanup
375 stack and optionally invoke it.
378 .Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
380 Push the specified cancellation cleanup handler onto the calling thread's
384 The default system libraries include
387 No additional libraries or CFLAGS are necessary to use this API.
389 .Xr pthread_cleanup_pop 3 ,
390 .Xr pthread_cleanup_push 3 ,
391 .Xr pthread_cond_broadcast 3 ,
392 .Xr pthread_cond_destroy 3 ,
393 .Xr pthread_cond_init 3 ,
394 .Xr pthread_cond_signal 3 ,
395 .Xr pthread_cond_timedwait 3 ,
396 .Xr pthread_cond_wait 3 ,
397 .Xr pthread_create 3 ,
398 .Xr pthread_detach 3 ,
399 .Xr pthread_equal 3 ,
401 .Xr pthread_getspecific 3 ,
403 .Xr pthread_key_delete 3 ,
404 .Xr pthread_mutex_destroy 3 ,
405 .Xr pthread_mutex_init 3 ,
406 .Xr pthread_mutex_lock 3 ,
407 .Xr pthread_mutex_trylock 3 ,
408 .Xr pthread_mutex_unlock 3 ,
410 .Xr pthread_rwlock_destroy 3 ,
411 .Xr pthread_rwlock_init 3 ,
412 .Xr pthread_rwlock_rdlock 3 ,
413 .Xr pthread_rwlock_unlock 3 ,
414 .Xr pthread_rwlock_wrlock 3 ,
415 .Xr pthread_rwlockattr_destroy 3 ,
416 .Xr pthread_rwlockattr_getpshared 3 ,
417 .Xr pthread_rwlockattr_init 3 ,
418 .Xr pthread_rwlockattr_setpshared 3 ,
420 .Xr pthread_setspecific 3
433 The functions in libc with the
437 suffix are non-portable extensions to POSIX threads.
439 The functions in libc with the
441 prefix are extensions created by The Open Group as part of the