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