xref: /freebsd/share/man/man3/pthread.3 (revision b5c508fba33452c36601090d080ddc688efad870)
10f7d6847SJulian Elischer.\" Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
20f7d6847SJulian Elischer.\" All rights reserved.
30f7d6847SJulian Elischer.\"
40f7d6847SJulian Elischer.\" Redistribution and use in source and binary forms, with or without
50f7d6847SJulian Elischer.\" modification, are permitted provided that the following conditions
60f7d6847SJulian Elischer.\" are met:
70f7d6847SJulian Elischer.\" 1. Redistributions of source code must retain the above copyright
80f7d6847SJulian Elischer.\"    notice, this list of conditions and the following disclaimer.
90f7d6847SJulian Elischer.\" 2. Redistributions in binary form must reproduce the above copyright
100f7d6847SJulian Elischer.\"    notice, this list of conditions and the following disclaimer in the
110f7d6847SJulian Elischer.\"    documentation and/or other materials provided with the distribution.
120f7d6847SJulian Elischer.\" 3. All advertising materials mentioning features or use of this software
130f7d6847SJulian Elischer.\"    must display the following acknowledgement:
140f7d6847SJulian Elischer.\"	This product includes software developed by John Birrell.
150f7d6847SJulian Elischer.\" 4. Neither the name of the author nor the names of any co-contributors
160f7d6847SJulian Elischer.\"    may be used to endorse or promote products derived from this software
170f7d6847SJulian Elischer.\"    without specific prior written permission.
180f7d6847SJulian Elischer.\"
190f7d6847SJulian Elischer.\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
200f7d6847SJulian Elischer.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
210f7d6847SJulian Elischer.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
220f7d6847SJulian Elischer.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
230f7d6847SJulian Elischer.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
240f7d6847SJulian Elischer.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
250f7d6847SJulian Elischer.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
260f7d6847SJulian Elischer.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
270f7d6847SJulian Elischer.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
280f7d6847SJulian Elischer.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
290f7d6847SJulian Elischer.\" SUCH DAMAGE.
300f7d6847SJulian Elischer.\"
317f3dea24SPeter Wemm.\" $FreeBSD$
32ac5afce1SJohn-Mark Gurney.\"
33d4a76febSAlexander Langer.Dd September 10, 1998
340f7d6847SJulian Elischer.Dt PTHREAD 3
350f7d6847SJulian Elischer.Os BSD 4
360f7d6847SJulian Elischer.Sh NAME
370f7d6847SJulian Elischer.Nm pthread
380f7d6847SJulian Elischer.Nd POSIX thread functions
390f7d6847SJulian Elischer.Sh DESCRIPTION
400f7d6847SJulian ElischerPOSIX threads are a set of functions that support applications with
410f7d6847SJulian Elischerrequirements for multiple flows of control, called
420f7d6847SJulian Elischer.Fa threads ,
436d249eeeSSheldon Hearnwithin a process.
446d249eeeSSheldon HearnMultithreading is used to improve the performance of a
450f7d6847SJulian Elischerprogram.
460f7d6847SJulian Elischer.Pp
470f7d6847SJulian ElischerThe POSIX thread functions are summarized in this section in the following
480f7d6847SJulian Elischergroups:
490f7d6847SJulian Elischer.Bl -bullet -offset indent
500f7d6847SJulian Elischer.It
51d4a76febSAlexander LangerThread Routines
520f7d6847SJulian Elischer.It
530f7d6847SJulian ElischerAttribute Object Routines
540f7d6847SJulian Elischer.It
550f7d6847SJulian ElischerMutex Routines
560f7d6847SJulian Elischer.It
570f7d6847SJulian ElischerCondition Variable Routines
580f7d6847SJulian Elischer.It
59d4a76febSAlexander LangerRead/Write Lock Routines
60d4a76febSAlexander Langer.It
610f7d6847SJulian ElischerPer-Thread Context Routines
620f7d6847SJulian Elischer.It
630f7d6847SJulian ElischerCleanup Routines
640f7d6847SJulian Elischer.El
650f7d6847SJulian Elischer.Sh THREAD ROUTINES
660f7d6847SJulian Elischer.Bl -tag -width Er
670f7d6847SJulian Elischer.It int Fn pthread_create "pthread_t *thread" "const pthread_attr_t *attr" "void *(*start_routine)(void *)" "void *arg"
680f7d6847SJulian ElischerCreates a new thread of execution.
690f7d6847SJulian Elischer.It int Fn pthread_detach "pthread_t thread"
700f7d6847SJulian ElischerMarks a thread for deletion.
710f7d6847SJulian Elischer.It int Fn pthread_equal "pthread_t t1" "pthread_t t2"
720f7d6847SJulian ElischerCompares two thread IDs.
730f7d6847SJulian Elischer.It void Fn pthread_exit "void *value_ptr"
740f7d6847SJulian ElischerTerminates the calling thread.
750f7d6847SJulian Elischer.It int Fn pthread_join "pthread_t thread" "void **value_ptr"
760f7d6847SJulian ElischerCauses the calling thread to wait for the termination of the specified thread.
77ddb9c6cdSChris Costello.It int Fn pthread_cancel "pthread_t thread"
78ddb9c6cdSChris CostelloCancels execution of a thread.
790f7d6847SJulian Elischer.It int Fn pthread_once "pthread_once_t *once_control" "void (*init_routine)(void)"
800f7d6847SJulian ElischerCalls an initialization routine once.
810f7d6847SJulian Elischer.It pthread_t Fn pthread_self void
820f7d6847SJulian ElischerReturns the thread ID of the calling thread.
830f7d6847SJulian Elischer.El
840f7d6847SJulian Elischer.Sh ATTRIBUTE OBJECT ROUTINES
850f7d6847SJulian Elischer.Bl -tag -width Er
860f7d6847SJulian Elischer.It int Fn pthread_attr_destroy "pthread_attr_t *attr"
870f7d6847SJulian ElischerDestroy a thread attributes object.
880f7d6847SJulian Elischer.It int Fn pthread_attr_getinheritsched "pthread_attr_t *attr" "int *inheritsched"
890f7d6847SJulian ElischerGet the inherit scheduling attribute from a thread attributes object.
900f7d6847SJulian Elischer.It int Fn pthread_attr_getschedparam "pthread_attr_t *attr" "struct sched_param *param"
910f7d6847SJulian ElischerGet the scheduling parameter attribute from a thread attributes object.
920f7d6847SJulian Elischer.It int Fn pthread_attr_getschedpolicy "pthread_attr_t *attr" "int *policy"
930f7d6847SJulian ElischerGet the scheduling policy attribute from a thread attributes object.
940f7d6847SJulian Elischer.It int Fn pthread_attr_getscope "pthread_attr_t *attr" "int *contentionscope"
950f7d6847SJulian ElischerGet the contention scope attribute from a thread attributes object.
960f7d6847SJulian Elischer.It int Fn pthread_attr_getstacksize "pthread_attr_t *attr" "size_t *stacksize"
970f7d6847SJulian ElischerGet the stack size attribute from a thread attributes object.
980f7d6847SJulian Elischer.It int Fn pthread_attr_getstackaddr "pthread_attr_t *attr" "void **stackaddr"
990f7d6847SJulian ElischerGet the stack address attribute from a thread attributes object.
1000f7d6847SJulian Elischer.It int Fn pthread_attr_getdetachstate "pthread_attr_t *attr" "int *detachstate"
1010f7d6847SJulian ElischerGet the detach state attribute from a thread attributes object.
1020f7d6847SJulian Elischer.It int Fn pthread_attr_init "pthread_attr_t *attr"
1030f7d6847SJulian ElischerInitialize a thread attributes object with default values.
1040f7d6847SJulian Elischer.It int Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
1050f7d6847SJulian ElischerSet the inherit scheduling attribute in a thread attributes object.
1060f7d6847SJulian Elischer.It int Fn pthread_attr_setschedparam "pthread_attr_t *attr" "struct sched_param *param"
1070f7d6847SJulian ElischerSet the scheduling parameter attribute in a thread attributes object.
1080f7d6847SJulian Elischer.It int Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
1090f7d6847SJulian ElischerSet the scheduling policy attribute in a thread attributes object.
1100f7d6847SJulian Elischer.It int Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
1110f7d6847SJulian ElischerSet the contention scope attribute in a thread attributes object.
1120f7d6847SJulian Elischer.It int Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
1130f7d6847SJulian ElischerSet the stack size attribute in a thread attributes object.
1140f7d6847SJulian Elischer.It int Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
1150f7d6847SJulian ElischerSet the stack address attribute in a thread attributes object.
1160f7d6847SJulian Elischer.It int Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
1170f7d6847SJulian ElischerSet the detach state in a thread attributes object.
1180f7d6847SJulian Elischer.El
1190f7d6847SJulian Elischer.Sh MUTEX ROUTINES
1200f7d6847SJulian Elischer.Bl -tag -width Er
1210f7d6847SJulian Elischer.It int Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
1220f7d6847SJulian ElischerDestroy a mutex attributes object.
1230f7d6847SJulian Elischer.It int Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
1240f7d6847SJulian ElischerInitialize a mutex attributes object with default values.
1250f7d6847SJulian Elischer.It int Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
1260f7d6847SJulian ElischerDestroy a mutex.
1270f7d6847SJulian Elischer.It int Fn pthread_mutex_init "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
128dfb9495bSTim VanderhoekInitialize a mutex with specified attributes.
1290f7d6847SJulian Elischer.It int Fn pthread_mutex_lock "pthread_mutex_t *mutex"
1300f7d6847SJulian ElischerLock a mutex and block until it becomes available.
1310f7d6847SJulian Elischer.It int Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
1320f7d6847SJulian ElischerTry to lock a mutex, but don't block if the mutex is locked by another thread,
1330f7d6847SJulian Elischerincluding the current thread.
1340f7d6847SJulian Elischer.It int Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
1350f7d6847SJulian ElischerUnlock a mutex.
1360f7d6847SJulian Elischer.El
1370f7d6847SJulian Elischer.Sh CONDITION VARIABLE ROUTINES
1380f7d6847SJulian Elischer.Bl -tag -width Er
1390f7d6847SJulian Elischer.It int Fn pthread_condattr_init "pthread_condattr_t *attr"
1400f7d6847SJulian ElischerInitialize a condition variable attributes object with default values.
1410f7d6847SJulian Elischer.It int Fn pthread_condattr_destroy "pthread_condattr_t *attr"
1420f7d6847SJulian ElischerDestroy a condition variable attributes object.
1430f7d6847SJulian Elischer.It int Fn pthread_cond_broadcast "pthread_cond_t *cond"
1440f7d6847SJulian ElischerUnblock all threads currently blocked on the specified condition variable.
1450f7d6847SJulian Elischer.It int Fn pthread_cond_destroy "pthread_cond_t *cond"
1460f7d6847SJulian ElischerDestroy a condition variable.
1470f7d6847SJulian Elischer.It int Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
1480f7d6847SJulian ElischerInitialize a condition variable with specified attributes.
1490f7d6847SJulian Elischer.It int Fn pthread_cond_signal "pthread_cond_t *cond"
1500f7d6847SJulian ElischerUnblock at least one of the threads blocked on the specified condition variable.
1510f7d6847SJulian Elischer.It int Fn pthread_cond_timedwait "pthread_cond_t *cond" "pthread_mutex_t *mutex" "const struct timespec *abstime"
1520f7d6847SJulian ElischerWait no longer than the specified time for a condition and lock the specified mutex.
1530f7d6847SJulian Elischer.It int Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
1540f7d6847SJulian ElischerWait for a condition and lock the specified mutex.
1550f7d6847SJulian Elischer.El
156d4a76febSAlexander Langer.Sh READ/WRITE LOCK ROUTINES
157d4a76febSAlexander Langer.Bl -tag -width Er
158d4a76febSAlexander Langer.It int Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
159d4a76febSAlexander LangerDestroy a read/write lock object.
160d4a76febSAlexander Langer.It int Fn pthread_rwlock_init "pthread_rwlock_t *lock" "const pthread_rwlockattr_t *attr"
161d4a76febSAlexander LangerInitialize a read/write lock object.
162d4a76febSAlexander Langer.It int Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
163d4a76febSAlexander LangerLock a read/write lock for reading, blocking until the lock can be
164d4a76febSAlexander Langeracquired.
165d4a76febSAlexander Langer.It int Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
166d4a76febSAlexander LangerAttempt to lock a read/write lock for reading, without blocking if the
167d4a76febSAlexander Langerlock is unavailable.
168d4a76febSAlexander Langer.It int Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
169d4a76febSAlexander LangerAttempt to lock a read/write lock for writing, without blocking if the
170d4a76febSAlexander Langerlock is unavailable.
171d4a76febSAlexander Langer.It int Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
172d4a76febSAlexander LangerUnlock a read/write lock.
173d4a76febSAlexander Langer.It int Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
174d4a76febSAlexander LangerLock a read/write lock for writing, blocking until the lock can be
175d4a76febSAlexander Langeracquired.
176d4a76febSAlexander Langer.It int Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
177d4a76febSAlexander LangerDestroy a read/write lock attribute object.
178d4a76febSAlexander Langer.It int Fn pthread_rwlockattr_getpshared "pthread_rwlockattr_t *attr" "int *pshared"
179d4a76febSAlexander LangerRetrieve the process shared setting for the read/write lock attribute
180d4a76febSAlexander Langerobject.
181d4a76febSAlexander Langer.It int Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
182d4a76febSAlexander LangerInitialize a read/write lock attribute object.
183d4a76febSAlexander Langer.It int Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int *pshared"
184d4a76febSAlexander LangerSet the process shared setting for the read/write lock attribute object.
185d4a76febSAlexander Langer.El
1860f7d6847SJulian Elischer.Sh PER-THREAD CONTEXT ROUTINES
1870f7d6847SJulian Elischer.Bl -tag -width Er
1880f7d6847SJulian Elischer.It int Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
1890f7d6847SJulian ElischerCreate a thread-specific data key.
1900f7d6847SJulian Elischer.It int Fn pthread_key_delete "pthread_key_t key"
1910f7d6847SJulian ElischerDelete a thread-specific data key.
1920f7d6847SJulian Elischer.It void * Fn pthread_getspecific "pthread_key_t key" "void **value_ptr"
1930f7d6847SJulian ElischerGet the thread-specific value for the specified key.
1940f7d6847SJulian Elischer.It int Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
1950f7d6847SJulian ElischerSet the thread-specific value for the specified key.
1960f7d6847SJulian Elischer.El
1970f7d6847SJulian Elischer.Sh CLEANUP ROUTINES
1980f7d6847SJulian Elischer.Bl -tag -width Er
1990f7d6847SJulian Elischer.It void Fn pthread_cleanup_pop "int execute"
2000f7d6847SJulian ElischerRemove the routine at the top of the calling thread's cancellation cleanup
2010f7d6847SJulian Elischerstack and optionally invoke it.
2020f7d6847SJulian Elischer.It void Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
2030f7d6847SJulian ElischerPush the specified cancellation cleanup handler onto the calling thread's
2040f7d6847SJulian Elischercancellation stack.
2050f7d6847SJulian Elischer.El
2060f7d6847SJulian Elischer.Sh INSTALLATION
207b5c508fbSRuslan ErmilovThe current
208b5c508fbSRuslan Ermilov.Fx
209b5c508fbSRuslan ErmilovPOSIX thread implementation is built in the library
2100f7d6847SJulian Elischer.Fa libc_r
2110f7d6847SJulian Elischerwhich contains both thread-safe libc functions and the thread functions.
2120f7d6847SJulian ElischerThis library replaces
2130f7d6847SJulian Elischer.Fa libc
2140f7d6847SJulian Elischerfor threaded applications.
2150f7d6847SJulian Elischer.Pp
2160f7d6847SJulian ElischerBy default,
2170f7d6847SJulian Elischer.Fa libc_r
218106115b3SSteve Priceis built as part of a 'make world'.  To disable the build of
2190f7d6847SJulian Elischer.Fa libc_r
220106115b3SSteve Priceyou must supply the '-DNOLIBC_R' option to
221106115b3SSteve Price.Xr make 1 .
222087ee430SJohn Birrell.Pp
223b5c508fbSRuslan ErmilovA
224b5c508fbSRuslan Ermilov.Fx
225b5c508fbSRuslan Ermilovspecific option has been added to gcc to make linking
226087ee430SJohn Birrellthreaded processes simple.
227087ee430SJohn Birrell.Fa gcc -pthread
228087ee430SJohn Birrelllinks a threaded process against
229087ee430SJohn Birrell.Fa libc_r
230087ee430SJohn Birrellinstead of
231087ee430SJohn Birrell.Fa libc .
232997d90beSBill Swingle.Sh SEE ALSO
233997d90beSBill Swingle.Xr pthread_create 3 ,
234997d90beSBill Swingle.Xr pthread_detach 3 ,
235997d90beSBill Swingle.Xr pthread_equal 3 ,
236997d90beSBill Swingle.Xr pthread_exit 3 ,
237997d90beSBill Swingle.Xr pthread_join 3 ,
238997d90beSBill Swingle.Xr pthread_once 3 ,
239997d90beSBill Swingle.Xr pthread_self 3 ,
240997d90beSBill Swingle.Xr pthread_mutex_destroy 3 ,
241997d90beSBill Swingle.Xr pthread_mutex_init 3 ,
242997d90beSBill Swingle.Xr pthread_mutex_lock 3 ,
243997d90beSBill Swingle.Xr pthread_mutex_trylock 3 ,
244997d90beSBill Swingle.Xr pthread_mutex_unlock 3 ,
245997d90beSBill Swingle.Xr pthread_cond_broadcast 3 ,
246997d90beSBill Swingle.Xr pthread_cond_destroy 3 ,
247997d90beSBill Swingle.Xr pthread_cond_init 3 ,
248997d90beSBill Swingle.Xr pthread_cond_signal 3 ,
249997d90beSBill Swingle.Xr pthread_cond_timedwait 3 ,
250997d90beSBill Swingle.Xr pthread_cond_wait 3 ,
251997d90beSBill Swingle.Xr pthread_rwlock_destroy 3 ,
252997d90beSBill Swingle.Xr pthread_rwlock_init 3 ,
253997d90beSBill Swingle.Xr pthread_rwlock_rdlock 3 ,
254997d90beSBill Swingle.Xr pthread_rwlock_unlock 3 ,
255997d90beSBill Swingle.Xr pthread_rwlock_wrlock 3 ,
256997d90beSBill Swingle.Xr pthread_rwlockattr_destroy 3 ,
257997d90beSBill Swingle.Xr pthread_rwlockattr_getpshared 3 ,
258997d90beSBill Swingle.Xr pthread_rwlockattr_init 3 ,
259997d90beSBill Swingle.Xr pthread_rwlockattr_setpshared 3 ,
260997d90beSBill Swingle.Xr pthread_key_delete 3 ,
261997d90beSBill Swingle.Xr pthread_getspecific 3 ,
262997d90beSBill Swingle.Xr pthread_setspecific 3 ,
263997d90beSBill Swingle.Xr pthread_cleanup_pop 3 ,
264997d90beSBill Swingle.Xr pthread_cleanup_push 3
2650f7d6847SJulian Elischer.Sh STANDARDS
2660f7d6847SJulian ElischerThe functions in
2670f7d6847SJulian Elischer.Fa libc_r
2680f7d6847SJulian Elischerwith the
2690f7d6847SJulian Elischer.Fa pthread_
270efc0456fSAlexander Langerprefix and not
2710f7d6847SJulian Elischer.Fa _np
272d4a76febSAlexander Langersuffix or
273d4a76febSAlexander Langer.Fa pthread_rwlock
274d4a76febSAlexander Langerprefix conform to IEEE
2750f7d6847SJulian Elischer.Pq Dq Tn POSIX
276087ee430SJohn BirrellStd 1003.1 Second Edition 1996-07-12
2770f7d6847SJulian Elischer.Pp
2780f7d6847SJulian ElischerThe functions in libc_r with the
2790f7d6847SJulian Elischer.Fa pthread_
2800f7d6847SJulian Elischerprefix and
2810f7d6847SJulian Elischer.Fa _np
2820f7d6847SJulian Elischersuffix are non-portable extensions to POSIX threads.
283d4a76febSAlexander Langer.Pp
284d4a76febSAlexander LangerThe functions in libc_r with the
285d4a76febSAlexander Langer.Fa pthread_rwlock
286d4a76febSAlexander Langerprefix are extensions created by The Open Group as part of the Single
287d4a76febSAlexander LangerUNIX Specification, Version 2.
288