xref: /freebsd/share/man/man3/pthread.3 (revision 0f7d684755aefbcfd70b45d24efc2d39bc505c4c)
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.\"
310f7d6847SJulian Elischer.Dd April 4, 1996
320f7d6847SJulian Elischer.Dt PTHREAD 3
330f7d6847SJulian Elischer.Os BSD 4
340f7d6847SJulian Elischer.Sh NAME
350f7d6847SJulian Elischer.Nm pthread
360f7d6847SJulian Elischer.Nd POSIX thread functions
370f7d6847SJulian Elischer.Sh DESCRIPTION
380f7d6847SJulian ElischerPOSIX threads are a set of functions that support applications with
390f7d6847SJulian Elischerrequirements for multiple flows of control, called
400f7d6847SJulian Elischer.Fa threads ,
410f7d6847SJulian Elischerwithin a process. Multithreading is used to improve the performance of a
420f7d6847SJulian Elischerprogram.
430f7d6847SJulian Elischer.Pp
440f7d6847SJulian ElischerThe POSIX thread functions are summarized in this section in the following
450f7d6847SJulian Elischergroups:
460f7d6847SJulian Elischer.Bl -bullet -offset indent
470f7d6847SJulian Elischer.It
480f7d6847SJulian ElischerThread routines
490f7d6847SJulian Elischer.It
500f7d6847SJulian ElischerAttribute Object Routines
510f7d6847SJulian Elischer.It
520f7d6847SJulian ElischerMutex Routines
530f7d6847SJulian Elischer.It
540f7d6847SJulian ElischerCondition Variable Routines
550f7d6847SJulian Elischer.It
560f7d6847SJulian ElischerPer-Thread Context Routines
570f7d6847SJulian Elischer.It
580f7d6847SJulian ElischerCleanup Routines
590f7d6847SJulian Elischer.El
600f7d6847SJulian Elischer.Sh THREAD ROUTINES
610f7d6847SJulian Elischer.Bl -tag -width Er
620f7d6847SJulian Elischer.It int Fn pthread_create "pthread_t *thread" "const pthread_attr_t *attr" "void *(*start_routine)(void *)" "void *arg"
630f7d6847SJulian ElischerCreates a new thread of execution.
640f7d6847SJulian Elischer.It int Fn pthread_detach "pthread_t thread"
650f7d6847SJulian ElischerMarks a thread for deletion.
660f7d6847SJulian Elischer.It int Fn pthread_equal "pthread_t t1" "pthread_t t2"
670f7d6847SJulian ElischerCompares two thread IDs.
680f7d6847SJulian Elischer.It void Fn pthread_exit "void *value_ptr"
690f7d6847SJulian ElischerTerminates the calling thread.
700f7d6847SJulian Elischer.It int Fn pthread_join "pthread_t thread" "void **value_ptr"
710f7d6847SJulian ElischerCauses the calling thread to wait for the termination of the specified thread.
720f7d6847SJulian Elischer.It int Fn pthread_once "pthread_once_t *once_control" "void (*init_routine)(void)"
730f7d6847SJulian ElischerCalls an initialization routine once.
740f7d6847SJulian Elischer.It pthread_t Fn pthread_self void
750f7d6847SJulian ElischerReturns the thread ID of the calling thread.
760f7d6847SJulian Elischer.El
770f7d6847SJulian Elischer.Sh ATTRIBUTE OBJECT ROUTINES
780f7d6847SJulian Elischer.Bl -tag -width Er
790f7d6847SJulian Elischer.It int Fn pthread_attr_destroy "pthread_attr_t *attr"
800f7d6847SJulian ElischerDestroy a thread attributes object.
810f7d6847SJulian Elischer.It int Fn pthread_attr_getinheritsched "pthread_attr_t *attr" "int *inheritsched"
820f7d6847SJulian ElischerGet the inherit scheduling attribute from a thread attributes object.
830f7d6847SJulian Elischer.It int Fn pthread_attr_getschedparam "pthread_attr_t *attr" "struct sched_param *param"
840f7d6847SJulian ElischerGet the scheduling parameter attribute from a thread attributes object.
850f7d6847SJulian Elischer.It int Fn pthread_attr_getschedpolicy "pthread_attr_t *attr" "int *policy"
860f7d6847SJulian ElischerGet the scheduling policy attribute from a thread attributes object.
870f7d6847SJulian Elischer.It int Fn pthread_attr_getscope "pthread_attr_t *attr" "int *contentionscope"
880f7d6847SJulian ElischerGet the contention scope attribute from a thread attributes object.
890f7d6847SJulian Elischer.It int Fn pthread_attr_getstacksize "pthread_attr_t *attr" "size_t *stacksize"
900f7d6847SJulian ElischerGet the stack size attribute from a thread attributes object.
910f7d6847SJulian Elischer.It int Fn pthread_attr_getstackaddr "pthread_attr_t *attr" "void **stackaddr"
920f7d6847SJulian ElischerGet the stack address attribute from a thread attributes object.
930f7d6847SJulian Elischer.It int Fn pthread_attr_getdetachstate "pthread_attr_t *attr" "int *detachstate"
940f7d6847SJulian ElischerGet the detach state attribute from a thread attributes object.
950f7d6847SJulian Elischer.It int Fn pthread_attr_init "pthread_attr_t *attr"
960f7d6847SJulian ElischerInitialize a thread attributes object with default values.
970f7d6847SJulian Elischer.It int Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
980f7d6847SJulian ElischerSet the inherit scheduling attribute in a thread attributes object.
990f7d6847SJulian Elischer.It int Fn pthread_attr_setschedparam "pthread_attr_t *attr" "struct sched_param *param"
1000f7d6847SJulian ElischerSet the scheduling parameter attribute in a thread attributes object.
1010f7d6847SJulian Elischer.It int Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
1020f7d6847SJulian ElischerSet the scheduling policy attribute in a thread attributes object.
1030f7d6847SJulian Elischer.It int Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
1040f7d6847SJulian ElischerSet the contention scope attribute in a thread attributes object.
1050f7d6847SJulian Elischer.It int Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
1060f7d6847SJulian ElischerSet the stack size attribute in a thread attributes object.
1070f7d6847SJulian Elischer.It int Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
1080f7d6847SJulian ElischerSet the stack address attribute in a thread attributes object.
1090f7d6847SJulian Elischer.It int Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
1100f7d6847SJulian ElischerSet the detach state in a thread attributes object.
1110f7d6847SJulian Elischer.El
1120f7d6847SJulian Elischer.Sh MUTEX ROUTINES
1130f7d6847SJulian Elischer.Bl -tag -width Er
1140f7d6847SJulian Elischer.It int Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
1150f7d6847SJulian ElischerDestroy a mutex attributes object.
1160f7d6847SJulian Elischer.It int Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
1170f7d6847SJulian ElischerInitialize a mutex attributes object with default values.
1180f7d6847SJulian Elischer.It int Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
1190f7d6847SJulian ElischerDestroy a mutex.
1200f7d6847SJulian Elischer.It int Fn pthread_mutex_init "pthread_mutex_t *mutex" "const pthread_mutexattr_t *attr"
1210f7d6847SJulian ElischerInitialise a mutex with specified attributes.
1220f7d6847SJulian Elischer.It int Fn pthread_mutex_lock "pthread_mutex_t *mutex"
1230f7d6847SJulian ElischerLock a mutex and block until it becomes available.
1240f7d6847SJulian Elischer.It int Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
1250f7d6847SJulian ElischerTry to lock a mutex, but don't block if the mutex is locked by another thread,
1260f7d6847SJulian Elischerincluding the current thread.
1270f7d6847SJulian Elischer.It int Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
1280f7d6847SJulian ElischerUnlock a mutex.
1290f7d6847SJulian Elischer.El
1300f7d6847SJulian Elischer.Sh CONDITION VARIABLE ROUTINES
1310f7d6847SJulian Elischer.Bl -tag -width Er
1320f7d6847SJulian Elischer.It int Fn pthread_condattr_init "pthread_condattr_t *attr"
1330f7d6847SJulian ElischerInitialize a condition variable attributes object with default values.
1340f7d6847SJulian Elischer.It int Fn pthread_condattr_destroy "pthread_condattr_t *attr"
1350f7d6847SJulian ElischerDestroy a condition variable attributes object.
1360f7d6847SJulian Elischer.It int Fn pthread_cond_broadcast "pthread_cond_t *cond"
1370f7d6847SJulian ElischerUnblock all threads currently blocked on the specified condition variable.
1380f7d6847SJulian Elischer.It int Fn pthread_cond_destroy "pthread_cond_t *cond"
1390f7d6847SJulian ElischerDestroy a condition variable.
1400f7d6847SJulian Elischer.It int Fn pthread_cond_init "pthread_cond_t *cond" "const pthread_condattr_t *attr"
1410f7d6847SJulian ElischerInitialize a condition variable with specified attributes.
1420f7d6847SJulian Elischer.It int Fn pthread_cond_signal "pthread_cond_t *cond"
1430f7d6847SJulian ElischerUnblock at least one of the threads blocked on the specified condition variable.
1440f7d6847SJulian Elischer.It int Fn pthread_cond_timedwait "pthread_cond_t *cond" "pthread_mutex_t *mutex" "const struct timespec *abstime"
1450f7d6847SJulian ElischerWait no longer than the specified time for a condition and lock the specified mutex.
1460f7d6847SJulian Elischer.It int Fn pthread_cond_wait "pthread_cond_t *" "pthread_mutex_t *mutex"
1470f7d6847SJulian ElischerWait for a condition and lock the specified mutex.
1480f7d6847SJulian Elischer.El
1490f7d6847SJulian Elischer.Sh PER-THREAD CONTEXT ROUTINES
1500f7d6847SJulian Elischer.Bl -tag -width Er
1510f7d6847SJulian Elischer.It int Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
1520f7d6847SJulian ElischerCreate a thread-specific data key.
1530f7d6847SJulian Elischer.It int Fn pthread_key_delete "pthread_key_t key"
1540f7d6847SJulian ElischerDelete a thread-specific data key.
1550f7d6847SJulian Elischer.It void * Fn pthread_getspecific "pthread_key_t key" "void **value_ptr"
1560f7d6847SJulian ElischerGet the thread-specific value for the specified key.
1570f7d6847SJulian Elischer.It int Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
1580f7d6847SJulian ElischerSet the thread-specific value for the specified key.
1590f7d6847SJulian Elischer.El
1600f7d6847SJulian Elischer.Sh CLEANUP ROUTINES
1610f7d6847SJulian Elischer.Bl -tag -width Er
1620f7d6847SJulian Elischer.It void Fn pthread_cleanup_pop "int execute"
1630f7d6847SJulian ElischerRemove the routine at the top of the calling thread's cancellation cleanup
1640f7d6847SJulian Elischerstack and optionally invoke it.
1650f7d6847SJulian Elischer.It void Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
1660f7d6847SJulian ElischerPush the specified cancellation cleanup handler onto the calling thread's
1670f7d6847SJulian Elischercancellation stack.
1680f7d6847SJulian Elischer.El
1690f7d6847SJulian Elischer.Sh INSTALLATION
1700f7d6847SJulian ElischerThe current FreeBSD POSIX thread implementation is built in the library
1710f7d6847SJulian Elischer.Fa libc_r
1720f7d6847SJulian Elischerwhich contains both thread-safe libc functions and the thread functions.
1730f7d6847SJulian ElischerThis library replaces
1740f7d6847SJulian Elischer.Fa libc
1750f7d6847SJulian Elischerfor threaded applications.
1760f7d6847SJulian Elischer.Pp
1770f7d6847SJulian ElischerBy default,
1780f7d6847SJulian Elischer.Fa libc_r
1790f7d6847SJulian Elischeris not built as part of a 'make world'. To build and install it, type:
1800f7d6847SJulian Elischer.Bl -item -offset indent
1810f7d6847SJulian Elischer.It
1820f7d6847SJulian Elischercd /usr/src/lib/libc_r
1830f7d6847SJulian Elischer.It
1840f7d6847SJulian Elischermake depend && make all && make install
1850f7d6847SJulian Elischer.El
1860f7d6847SJulian Elischer.Pp
1870f7d6847SJulian ElischerThis assumes you have a full source tree below /usr/src and that you have at
1880f7d6847SJulian Elischerleast installed the header files in the way that 'make world' does.
1890f7d6847SJulian Elischer.Pp
1900f7d6847SJulian ElischerIf you wish to install
1910f7d6847SJulian Elischer.Fa libc_r
1920f7d6847SJulian Elischeras part of 'make world', type:
1930f7d6847SJulian Elischer.Bl -item -offset indent
1940f7d6847SJulian Elischer.It
1950f7d6847SJulian Elischercd /usr/src
1960f7d6847SJulian Elischer.It
1970f7d6847SJulian Elischermake -DWANT_LIBC_R world
1980f7d6847SJulian Elischer.El
1990f7d6847SJulian Elischer.Sh STANDARDS
2000f7d6847SJulian ElischerThe functions in
2010f7d6847SJulian Elischer.Fa libc_r
2020f7d6847SJulian Elischerwith the
2030f7d6847SJulian Elischer.Fa pthread_
2040f7d6847SJulian Elischerprefix and no
2050f7d6847SJulian Elischer.Fa _np
2060f7d6847SJulian Elischersuffix are expected to conform to IEEE
2070f7d6847SJulian Elischer.Pq Dq Tn POSIX
2080f7d6847SJulian ElischerStd 1003.1c when it is published.
2090f7d6847SJulian Elischer.Pp
2100f7d6847SJulian ElischerThe functions in libc_r with the
2110f7d6847SJulian Elischer.Fa pthread_
2120f7d6847SJulian Elischerprefix and
2130f7d6847SJulian Elischer.Fa _np
2140f7d6847SJulian Elischersuffix are non-portable extensions to POSIX threads.
215