1 .\" Copyright (c) 1996 John Birrell <jb@cimlogic.com.au>.
2 .\" All rights reserved.
4 .\" Redistribution and use in source and binary forms, with or without
5 .\" modification, are permitted provided that the following conditions
7 .\" 1. Redistributions of source code must retain the above copyright
8 .\" notice, this list of conditions and the following disclaimer.
9 .\" 2. Redistributions in binary form must reproduce the above copyright
10 .\" notice, this list of conditions and the following disclaimer in the
11 .\" documentation and/or other materials provided with the distribution.
12 .\" 3. All advertising materials mentioning features or use of this software
13 .\" must display the following acknowledgement:
14 .\" This product includes software developed by John Birrell.
15 .\" 4. Neither the name of the author nor the names of any co-contributors
16 .\" may be used to endorse or promote products derived from this software
17 .\" without specific prior written permission.
19 .\" THIS SOFTWARE IS PROVIDED BY JOHN BIRRELL AND CONTRIBUTORS ``AS IS'' AND
20 .\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 .\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 .\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 .\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 .\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 .\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 .\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 .\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 .\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 .\" $FreeBSD: src/share/man/man3/pthread.3,v 1.33 2009/04/01 08:08:25 trhodes Exp $
38 .Nd POSIX thread functions
44 POSIX threads are a set of functions that support applications with
45 requirements for multiple flows of control, called
48 Multithreading is used to improve the performance of a
51 The POSIX thread functions are summarized in this section in the following
54 .Bl -bullet -offset indent -compact
58 Attribute Object Routines
62 Condition Variable Routines
64 Read/Write Lock Routines
70 Per-Thread Context Routines
75 .Bl -tag -width indent
79 .Fa "pthread_t * restrict thread" "const pthread_attr_t * restrict attr"
80 .Fa "void *(*start_routine)(void *)" "void * restrict arg"
83 Creates a new thread of execution.
86 .Fn pthread_cancel "pthread_t thread"
88 Cancels execution of a thread.
91 .Fn pthread_detach "pthread_t thread"
93 Marks a thread for deletion.
96 .Fn pthread_equal "pthread_t t1" "pthread_t t2"
98 Compares two thread IDs.
101 .Fn pthread_exit "void *value_ptr"
103 Terminates the calling thread.
106 .Fn pthread_getconcurrency void
108 Get the current thread's level of concurrency.
111 .Fn pthread_getcpuclockid "pthread_t thread" "clockid_t *clock_id"
113 Access a specific thread's CPU-time clock.
116 .Fn pthread_getschedparam "pthread_t thread" "int * restrict policy" "struct sched_param * restrict param"
118 Get the scheduling parameters of a specific thread.
121 .Fn pthread_join "pthread_t thread" "void **value_ptr"
123 Causes the calling thread to wait for the termination of the specified thread.
126 .Fn pthread_kill "pthread_t thread" "int sig"
128 Delivers a signal to a specified thread.
131 .Fn pthread_once "pthread_once_t *once_control" "void (*init_routine)(void)"
133 Calls an initialization routine once.
136 .Fn pthread_self void
138 Returns the thread ID of the calling thread.
141 .Fn pthread_setcancelstate "int state" "int *oldstate"
143 Sets the current thread's cancelability state.
146 .Fn pthread_setcanceltype "int type" "int *oldtype"
148 Sets the current thread's cancelability type.
151 .Fn pthread_setconcurrency "int new_level"
153 Set the current thread's level of concurrency.
156 .Fn pthread_setschedparam "pthread_t thread" "int policy" "struct sched_param *param"
158 Set the scheduling parameters for a specific thread.
161 .Fn pthread_sigmask "int how" "const sigset_t * restrict set" "sigset_t * restrict oset"
163 Examine and/or change the calling thread's signal mask.
166 .Fn pthread_testcancel void
168 Creates a cancellation point in the calling thread.
171 .Fn pthread_yield void
173 Allows the scheduler to run another thread instead of the current one.
175 .Ss Attribute Object Routines
176 .Bl -tag -width indent
179 .Fn pthread_attr_destroy "pthread_attr_t *attr"
181 Destroy a thread attributes object.
184 .Fo pthread_attr_getguardsize
185 .Fa "const pthread_attr_t * restrict attr" "size_t * restrict guardsize"
188 Get the guard size attribute from a thread attributes object.
191 .Fo pthread_attr_getinheritsched
192 .Fa "const pthread_attr_t * restrict attr" "int * restrict inheritsched"
195 Get the inherit scheduling attribute from a thread attributes object.
198 .Fo pthread_attr_getschedparam
199 .Fa "const pthread_attr_t * restrict attr" "struct sched_param * restrict param"
202 Get the scheduling parameter attribute from a thread attributes object.
205 .Fn pthread_attr_getschedpolicy "const pthread_attr_t * restrict attr" "int * restrict policy"
207 Get the scheduling policy attribute from a thread attributes object.
210 .Fn pthread_attr_getscope "const pthread_attr_t * restrict attr" "int * restrict contentionscope"
212 Get the contention scope attribute from a thread attributes object.
215 .Fn pthread_attr_getstack "const pthread_attr_t * restrict attr" "void ** restrict stackaddr" "size_t * restrict stacksize"
217 Get the stack attributes from a thread attributes object.
220 .Fn pthread_attr_getstacksize "const pthread_attr_t * restrict attr" "size_t * restrict stacksize"
222 Get the stack size attribute from a thread attributes object.
225 .Fn pthread_attr_getstackaddr "const pthread_attr_t *attr" "void **stackaddr"
227 Get the stack address attribute from a thread attributes object.
230 .Fn pthread_attr_getdetachstate "const pthread_attr_t *attr" "int *detachstate"
232 Get the detach state attribute from a thread attributes object.
235 .Fn pthread_attr_init "pthread_attr_t *attr"
237 Initialize a thread attributes object with default values.
240 .Fo pthread_attr_setguardsize
241 .Fa "pthread_attr_t *attr" "size_t guardsize"
244 Set the guard size attribute in a thread attributes object.
247 .Fn pthread_attr_setinheritsched "pthread_attr_t *attr" "int inheritsched"
249 Set the inherit scheduling attribute in a thread attributes object.
252 .Fo pthread_attr_setschedparam
253 .Fa "pthread_attr_t * restrict attr" "const struct sched_param * restrict param"
256 Set the scheduling parameter attribute in a thread attributes object.
259 .Fn pthread_attr_setschedpolicy "pthread_attr_t *attr" "int policy"
261 Set the scheduling policy attribute in a thread attributes object.
264 .Fn pthread_attr_setscope "pthread_attr_t *attr" "int contentionscope"
266 Set the contention scope attribute in a thread attributes object.
269 .Fn pthread_attr_setstack "pthread_attr_t *attr" "void *stackaddr" "size_t stacksize"
271 Set the stack attributes in a thread attributes object.
274 .Fn pthread_attr_setstacksize "pthread_attr_t *attr" "size_t stacksize"
276 Set the stack size attribute in a thread attributes object.
279 .Fn pthread_attr_setstackaddr "pthread_attr_t *attr" "void *stackaddr"
281 Set the stack address attribute in a thread attributes object.
284 .Fn pthread_attr_setdetachstate "pthread_attr_t *attr" "int detachstate"
286 Set the detach state in a thread attributes object.
289 .Bl -tag -width indent
292 .Fn pthread_mutexattr_destroy "pthread_mutexattr_t *attr"
294 Destroy a mutex attributes object.
297 .Fn pthread_mutexattr_getprioceiling "const pthread_mutexattr_t * restrict attr" "int * restrict ceiling"
299 Obtain priority ceiling attribute of mutex attribute object.
302 .Fn pthread_mutexattr_getprotocol "const pthread_mutexattr_t * restrict attr" "int * restrict protocol"
304 Obtain protocol attribute of mutex attribute object.
307 .Fn pthread_mutexattr_getpshared "const pthread_mutexattr_t * restrict attr" "int * restrict pshared"
309 Get the process shared setting from a mutex attributes object.
312 .Fn pthread_mutexattr_gettype "const pthread_mutexattr_t * restrict attr" "int * restrict type"
314 Obtain the mutex type attribute in the specified mutex attributes object.
317 .Fn pthread_mutexattr_init "pthread_mutexattr_t *attr"
319 Initialize a mutex attributes object with default values.
322 .Fn pthread_mutexattr_setprioceiling "pthread_mutexattr_t *attr" "int ceiling"
324 Set priority ceiling attribute of mutex attribute object.
327 .Fn pthread_mutexattr_setprotocol "pthread_mutexattr_t *attr" "int protocol"
329 Set protocol attribute of mutex attribute object.
332 .Fn pthread_mutexattr_setpshared "pthread_mutexattr_t *attr" "int pshared"
334 Set the process shared setting in a mutex attributes object.
337 .Fn pthread_mutexattr_settype "pthread_mutexattr_t *attr" "int type"
339 Set the mutex type attribute that is used when a mutex is created.
342 .Fn pthread_mutex_destroy "pthread_mutex_t *mutex"
347 .Fn pthread_mutex_getprioceiling "const pthread_mutex_t * restrict mutex" "int * restrict ceiling"
349 Get the priority ceiling of a mutex.
352 .Fo pthread_mutex_init
353 .Fa "pthread_mutex_t * restrict mutex" "const pthread_mutexattr_t * restrict attr"
356 Initialize a mutex with specified attributes.
359 .Fn pthread_mutex_lock "pthread_mutex_t *mutex"
361 Lock a mutex and block until it becomes available.
364 .Fn pthread_mutex_setprioceiling "pthread_mutex_t * restrict mutex" "int ceiling" "int * restrict old_ceiling"
366 Set the priority ceiling of a mutex.
369 .Fo pthread_mutex_timedlock
370 .Fa "pthread_mutex_t * restrict mutex" "const struct timespec * restrict abstime"
373 Lock a mutex and block until it becomes available or until the timeout expires.
376 .Fn pthread_mutex_trylock "pthread_mutex_t *mutex"
378 Try to lock a mutex, but do not block if the mutex is locked by another thread,
379 including the current thread.
382 .Fn pthread_mutex_unlock "pthread_mutex_t *mutex"
386 .Ss Condition Variable Routines
387 .Bl -tag -width indent
390 .Fn pthread_condattr_destroy "pthread_condattr_t *attr"
392 Destroy a condition variable attributes object.
395 .Fn pthread_condattr_getclock "const pthread_condattr_t * restrict attr" "clockid_t * restrict clock_id"
397 Get the clock selection attribute from a condition variable attributes object.
400 .Fn pthread_condattr_init "pthread_condattr_t *attr"
402 Initialize a condition variable attributes object with default values.
405 .Fn pthread_condattr_getpshared "const pthread_condattr_t * restrict attr" "int * restrict pshared"
407 Get the process shared setting from a condition variable attributes object.
410 .Fn pthread_condattr_setclock "pthread_condattr_t *attr" "clockid_t clock_id"
412 Set the clock selection attribute in a condition variable attributes object.
415 .Fn pthread_condattr_setpshared "pthread_condattr_t *attr" "int pshared"
417 Set the process shared setting in a condition variable attributes object.
420 .Fn pthread_cond_broadcast "pthread_cond_t *cond"
422 Unblock all threads currently blocked on the specified condition variable.
425 .Fn pthread_cond_destroy "pthread_cond_t *cond"
427 Destroy a condition variable.
430 .Fn pthread_cond_init "pthread_cond_t * restrict cond" "const pthread_condattr_t * restrict attr"
432 Initialize a condition variable with specified attributes.
435 .Fn pthread_cond_signal "pthread_cond_t *cond"
437 Unblock at least one of the threads blocked on the specified condition variable.
440 .Fo pthread_cond_timedwait
441 .Fa "pthread_cond_t * restrict cond" "pthread_mutex_t * restrict mutex"
442 .Fa "const struct timespec * restrict abstime"
445 Wait no longer than the specified time for a condition
446 and lock the specified mutex.
449 .Fn pthread_cond_wait "pthread_cond_t * restrict cond" "pthread_mutex_t * restrict mutex"
451 Wait for a condition and lock the specified mutex.
453 .Ss Read/Write Lock Routines
454 .Bl -tag -width indent
457 .Fn pthread_rwlock_destroy "pthread_rwlock_t *lock"
459 Destroy a read/write lock object.
462 .Fo pthread_rwlock_init
463 .Fa "pthread_rwlock_t * restrict lock" "const pthread_rwlockattr_t * restrict attr"
466 Initialize a read/write lock object.
469 .Fn pthread_rwlock_rdlock "pthread_rwlock_t *lock"
471 Lock a read/write lock for reading, blocking until the lock can be
475 .Fn pthread_rwlock_timedrdlock "pthread_rwlock_t * restrict lock" "const struct timespec * restrict abstime"
477 Lock a read/write lock for reading with a timeout.
480 .Fn pthread_rwlock_timedwrlock "pthread_rwlock_t * restrict lock" "const struct timespec * restrict abstime"
482 Lock a read/write lock for writing with a timeout.
485 .Fn pthread_rwlock_tryrdlock "pthread_rwlock_t *lock"
487 Attempt to lock a read/write lock for reading, without blocking if the
491 .Fn pthread_rwlock_trywrlock "pthread_rwlock_t *lock"
493 Attempt to lock a read/write lock for writing, without blocking if the
497 .Fn pthread_rwlock_unlock "pthread_rwlock_t *lock"
499 Unlock a read/write lock.
502 .Fn pthread_rwlock_wrlock "pthread_rwlock_t *lock"
504 Lock a read/write lock for writing, blocking until the lock can be
508 .Fn pthread_rwlockattr_destroy "pthread_rwlockattr_t *attr"
510 Destroy a read/write lock attribute object.
513 .Fo pthread_rwlockattr_getpshared
514 .Fa "const pthread_rwlockattr_t * restrict attr" "int * restrict pshared"
517 Retrieve the process shared setting for the read/write lock attribute
521 .Fn pthread_rwlockattr_init "pthread_rwlockattr_t *attr"
523 Initialize a read/write lock attribute object.
526 .Fn pthread_rwlockattr_setpshared "pthread_rwlockattr_t *attr" "int pshared"
528 Set the process shared setting for the read/write lock attribute object.
530 .Ss Spin Lock Routines
531 .Bl -tag -width indent
534 .Fn pthread_spin_destroy "pthread_spinlock_t *lock"
536 Destroy a spin lock object.
539 .Fn pthread_spin_init "pthread_spinlock_t *lock" "int pshared"
541 Initialize a spin lock object.
544 .Fn pthread_spin_lock "pthread_spinlock_t *lock"
546 Lock a spin lock object.
549 .Fn pthread_spin_trylock "pthread_spinlock_t *lock"
551 Lock a spin lock object if it is not held.
554 .Fn pthread_spin_unlock "pthread_spinlock_t *lock"
556 Unlock a spin lock object.
559 .Bl -tag -width indent
562 .Fn pthread_barrier_destroy "pthread_barrier_t *barrier"
564 Delete a barrier and its resources.
567 .Fn pthread_barrier_init "pthread_barrier_t * restrict barrier" "const pthread_barrierattr_t * restrict attr" "unsigned count"
569 Initialize a barrier with the specified attribute object.
572 .Fn pthread_barrier_wait "pthread_barrier_t *barrier"
574 Block the calling threads until the barrier count is reached.
577 .Fn pthread_barrierattr_destroy "pthread_barrierattr_t *attr"
579 Destroy a barrier attribute object.
582 .Fn pthread_barrierattr_getpshared "const pthread_barrierattr_t * restrict attr" "int * restrict pshared"
584 Get the process-shared attribute from a barrier attribute object.
587 .Fn pthread_barrierattr_init "pthread_barrierattr_t *attr"
589 Initialize a barrier attribute object.
592 .Fn pthread_barrierattr_setpshared "pthread_barrierattr_t *attr" "int pshared"
594 Set the process-shared attribute in a barrier attribute object.
596 .Ss Per-Thread Context Routines
597 .Bl -tag -width indent
600 .Fn pthread_key_create "pthread_key_t *key" "void (*routine)(void *)"
602 Create a thread-specific data key.
605 .Fn pthread_key_delete "pthread_key_t key"
607 Delete a thread-specific data key.
610 .Fn pthread_getspecific "pthread_key_t key"
612 Get the thread-specific value for the specified key.
615 .Fn pthread_setspecific "pthread_key_t key" "const void *value_ptr"
617 Set the thread-specific value for the specified key.
620 .Bl -tag -width indent
624 .Fa "void (*prepare)(void)"
625 .Fa "void (*parent)(void)"
626 .Fa "void (*child)(void)"
629 Register fork handlers
632 .Fn pthread_cleanup_pop "int execute"
634 Remove the routine at the top of the calling thread's cancellation cleanup
635 stack and optionally invoke it.
638 .Fn pthread_cleanup_push "void (*routine)(void *)" "void *routine_arg"
640 Push the specified cancellation cleanup handler onto the calling thread's
643 .Sh IMPLEMENTATION NOTES
646 POSIX thread implementation is built in the library
648 which contains both thread-safe libc functions and the thread functions.
652 it is possible to switch the threading library used by dynamically linked
653 binaries at execution time by re-linking
654 .Pa /usr/lib/libpthread.so.x
655 to a different library in
656 .Pa /usr/lib/thread .
665 (or a higher major, if there were ABI changes).
668 is usually a symlink to
676 is installed, programs will still continue to use
678 and not follow the symlink
680 to the newer (and possibly incompatible)
684 is approximately the opposite:
686 is not a symlink but nevertheless has its
689 .Pa libpthread.so.0 .
690 .Pa libpthread.so.0 ,
691 however, is a symlink to the threading library of the user's choice.
692 The linker will use the default threading library which
694 is linked to, but the runtime linker will instead follow the symlink.
697 defines all pthread functions as weak symbols except for
699 (which is defined by libpthread.so.x to satisfy
703 will resolve all these references to the strong symbols from the thread
710 .\"can be used to override the system's default threading library.
714 specific option exists in
716 to simplify the linking of threaded processes.
718 links a threaded process against
723 .Xr pthread_atfork 3 ,
724 .Xr pthread_attr_destroy 3 ,
725 .Xr pthread_attr_getdetachstate 3 ,
726 .Xr pthread_attr_getguardsize 3 ,
727 .Xr pthread_attr_getinheritsched 3 ,
728 .Xr pthread_attr_getschedparam 3 ,
729 .Xr pthread_attr_getschedpolicy 3 ,
730 .Xr pthread_attr_getscope 3 ,
731 .Xr pthread_attr_getstack 3 ,
732 .Xr pthread_attr_getstackaddr 3 ,
733 .Xr pthread_attr_getstacksize 3 ,
734 .Xr pthread_attr_init 3 ,
735 .Xr pthread_attr_setdetachstate 3 ,
736 .Xr pthread_attr_setguardsize 3 ,
737 .Xr pthread_attr_setinheritsched 3 ,
738 .Xr pthread_attr_setschedparam 3 ,
739 .Xr pthread_attr_setschedpolicy 3 ,
740 .Xr pthread_attr_setscope 3 ,
741 .Xr pthread_attr_setstack 3 ,
742 .Xr pthread_attr_setstackaddr 3 ,
743 .Xr pthread_attr_setstacksize 3 ,
744 .Xr pthread_barrierattr_destroy 3 ,
745 .Xr pthread_barrierattr_getpshared 3 ,
746 .Xr pthread_barrierattr_init 3 ,
747 .Xr pthread_barrierattr_setpshared 3 ,
748 .Xr pthread_barrier_destroy 3 ,
749 .Xr pthread_barrier_init 3 ,
750 .Xr pthread_barrier_wait 3 ,
751 .Xr pthread_cancel 3 ,
752 .Xr pthread_cleanup_pop 3 ,
753 .Xr pthread_cleanup_push 3 ,
754 .Xr pthread_condattr_destroy 3 ,
755 .Xr pthread_condattr_getclock 3 ,
756 .Xr pthread_condattr_getpshared 3 ,
757 .Xr pthread_condattr_init 3 ,
758 .Xr pthread_condattr_setclock 3 ,
759 .Xr pthread_condattr_setpshared 3 ,
760 .Xr pthread_cond_broadcast 3 ,
761 .Xr pthread_cond_destroy 3 ,
762 .Xr pthread_cond_init 3 ,
763 .Xr pthread_cond_signal 3 ,
764 .Xr pthread_cond_timedwait 3 ,
765 .Xr pthread_cond_wait 3 ,
766 .Xr pthread_create 3 ,
767 .Xr pthread_detach 3 ,
768 .Xr pthread_equal 3 ,
770 .Xr pthread_getconcurrency 3 ,
771 .Xr pthread_getcpuclockid 3 ,
772 .Xr pthread_getschedparam 3 ,
773 .Xr pthread_getspecific 3 ,
775 .Xr pthread_key_create 3 ,
776 .Xr pthread_key_delete 3 ,
778 .Xr pthread_mutexattr_destroy 3 ,
779 .Xr pthread_mutexattr_getprioceiling 3 ,
780 .Xr pthread_mutexattr_getprotocol 3 ,
781 .Xr pthread_mutexattr_getpshared 3 ,
782 .Xr pthread_mutexattr_gettype 3 ,
783 .Xr pthread_mutexattr_init 3 ,
784 .Xr pthread_mutexattr_setprioceiling 3 ,
785 .Xr pthread_mutexattr_setprotocol 3 ,
786 .Xr pthread_mutexattr_setpshared 3 ,
787 .Xr pthread_mutexattr_settype 3 ,
788 .Xr pthread_mutex_destroy 3 ,
789 .Xr pthread_mutex_getprioceiling 3 ,
790 .Xr pthread_mutex_init 3 ,
791 .Xr pthread_mutex_lock 3 ,
792 .Xr pthread_mutex_setprioceiling 3 ,
793 .Xr pthread_mutex_timedlock 3 ,
794 .Xr pthread_mutex_trylock 3 ,
795 .Xr pthread_mutex_unlock 3 ,
797 .Xr pthread_rwlockattr_destroy 3 ,
798 .Xr pthread_rwlockattr_getpshared 3 ,
799 .Xr pthread_rwlockattr_init 3 ,
800 .Xr pthread_rwlockattr_setpshared 3 ,
801 .Xr pthread_rwlock_destroy 3 ,
802 .Xr pthread_rwlock_init 3 ,
803 .Xr pthread_rwlock_rdlock 3 ,
804 .Xr pthread_rwlock_timedrdlock 3 ,
805 .Xr pthread_rwlock_timedwrlock 3 ,
806 .Xr pthread_rwlock_tryrdlock 3 ,
807 .Xr pthread_rwlock_trywrlock 3 ,
808 .Xr pthread_rwlock_unlock 3 ,
809 .Xr pthread_rwlock_wrlock 3 ,
811 .Xr pthread_setcancelstate 3 ,
812 .Xr pthread_setcanceltype 3 ,
813 .Xr pthread_setconcurrency 3 ,
814 .Xr pthread_setschedparam 3 ,
815 .Xr pthread_setspecific 3 ,
816 .Xr pthread_sigmask 3 ,
817 .Xr pthread_spin_destroy 3 ,
818 .Xr pthread_spin_init 3 ,
819 .Xr pthread_spin_lock 3 ,
820 .Xr pthread_spin_trylock 3 ,
821 .Xr pthread_spin_unlock 3 ,
822 .Xr pthread_testcancel 3 ,
825 The functions with the
834 The functions with the
838 suffix are non-portable extensions to POSIX threads.
840 The functions with the
842 prefix are extensions created by The Open Group as part of the