1.\" $OpenBSD: pthreads.3,v 1.36 2012/05/03 09:07:17 pirofti Exp $ 2.\" David Leonard <d@openbsd.org>, 1998. Public domain. 3.Dd $Mdocdate: May 3 2012 $ 4.Dt PTHREADS 3 5.Os 6.Sh NAME 7.Nm pthreads 8.Nd POSIX 1003.1c thread interface 9.Sh DESCRIPTION 10A thread is a flow of control within a process. 11Each thread represents a minimal amount of state; 12normally just the CPU state and a signal mask. 13All other process state (such as memory, file descriptors) 14is shared among all of the threads in the process. 15.Pp 16In 17.Ox , 18threads are implemented in a user-level library. 19A program using these routines must be linked with the 20.Fl pthread 21option. 22.Pp 23The 24.Dv SIGINFO 25signal can be sent to a threaded process to have the library show the state of 26all of its threads. 27The information is sent to the process' 28controlling tty and describes each thread's 29ID, 30state (see 31.Sx Thread states ) , 32current priority, 33flags (see 34.Sx Thread flags ) , 35signal mask, and name (as set by 36.Xr pthread_set_name_np 3 ) . 37If the environment variable 38.Ev PTHREAD_DEBUG 39is defined additional information is displayed. 40.Pp 41For the purpose of this document, 42the functions available are grouped in the following categories. 43For further information, see the individual man page for each function. 44.Pp 45.Bl -dash -offset indent -compact 46.It 47Attribute Object Routines 48.It 49Cleanup Routines 50.It 51Condition Variable Routines 52.It 53Mutex Routines 54.It 55Non Portable Extensions 56.It 57Per-Thread Context Routines 58.It 59Read/Write Lock Routines 60.It 61Thread Routines 62.El 63.Ss Attribute Object Routines 64The functions available are as follows: 65.Pp 66.Bl -tag -width "pthread_attr_getdetachstate()" -compact 67.It Fn pthread_attr_init 68Initialise a threads attribute object. 69.It Fn pthread_attr_destroy 70Destroy a threads attribute object. 71.It Fn pthread_attr_getdetachstate 72Get detachstate attribute. 73.It Fn pthread_attr_setdetachstate 74Set detachstate attribute. 75.It Fn pthread_attr_getstack 76Get stackaddr and stacksize attributes. 77.It Fn pthread_attr_setstack 78Set stackaddr and stacksize attributes. 79.It Fn pthread_attr_getstackaddr 80Get stackaddr attribute. 81.It Fn pthread_attr_setstackaddr 82Set stackaddr attribute. 83.It Fn pthread_attr_getstacksize 84Get stacksize attribute. 85.It Fn pthread_attr_setstacksize 86Set stacksize attribute. 87.It Fn pthread_attr_getguardsize 88Get guardsize attribute. 89.It Fn pthread_attr_setguardsize 90Set guardsize attribute. 91.El 92.Ss Cleanup Routines 93The functions available are as follows: 94.Pp 95.Bl -tag -width "pthread_cleanup_push()" -compact 96.It Fn pthread_atfork 97Register fork handlers. 98.It Fn pthread_cleanup_pop 99Call the first cleanup routine. 100.It Fn pthread_cleanup_push 101Add a cleanup function for thread exit. 102.El 103.Ss Condition Variable Routines 104The functions available are as follows: 105.Pp 106.Bl -tag -width "pthread_condattr_setclock()" -compact 107.It Fn pthread_cond_init 108Create a condition variable. 109.It Fn pthread_cond_destroy 110Destroy a condition variable. 111.It Fn pthread_cond_broadcast 112Unblock all threads waiting for a condition variable. 113.It Fn pthread_cond_signal 114Unblock a thread waiting for a condition variable. 115.It Fn pthread_cond_timedwait 116Wait on a condition variable until a specific point in time. 117.It Fn pthread_cond_wait 118Wait on a condition variable. 119.It Fn pthread_condattr_init 120Initialise a condition variable attribute object. 121.It Fn pthread_condattr_destroy 122Destroy a condition variable attribute object. 123.It Fn pthread_condattr_getclock 124Get clock attribute. 125.It Fn pthread_condattr_setclock 126Set clock attribute. 127.El 128.Ss Mutex Routines 129The functions available are as follows: 130.Pp 131.Bl -tag -width "pthread_mutexattr_getprioceiling()" -compact 132.It Fn pthread_mutex_init 133Create a mutex. 134.It Fn pthread_mutex_destroy 135Free resources allocated for a mutex. 136.It Fn pthread_mutex_lock 137Lock a mutex. 138.It Fn pthread_mutex_timedlock 139Attempt to lock a mutex before a specific point in time. 140.It Fn pthread_mutex_trylock 141Attempt to lock a mutex without blocking. 142.It Fn pthread_mutex_unlock 143Unlock a mutex. 144.It Fn pthread_mutexattr_init 145Mutex attribute operations. 146.It Fn pthread_mutexattr_destroy 147Mutex attribute operations. 148.It Fn pthread_mutexattr_getprioceiling 149Mutex attribute operations. 150.It Fn pthread_mutexattr_setprioceiling 151Mutex attribute operations. 152.It Fn pthread_mutexattr_getprotocol 153Mutex attribute operations. 154.It Fn pthread_mutexattr_setprotocol 155Mutex attribute operations. 156.It Fn pthread_mutexattr_gettype 157Mutex attribute operations. 158.It Fn pthread_mutexattr_settype 159Mutex attribute operations. 160.El 161.Ss Non Portable Extensions 162The functions available are as follows: 163.Pp 164.Bl -tag -width "pthread_set_name_np()" -compact 165.It Fn pthread_main_np 166Identify the main thread. 167.It Fn pthread_set_name_np 168Set the name of a thread. 169.It Fn pthread_stackseg_np 170Return stack size and location. 171.It Fn pthread_yield 172Yield control of the current thread. 173.El 174.Ss Per-Thread Context Routines 175The functions available are as follows: 176.Pp 177.Bl -tag -width "pthread_getspecific()" -compact 178.It Fn pthread_key_create 179Thread-specific data key creation. 180.It Fn pthread_key_delete 181Delete a thread-specific data key. 182.It Fn pthread_getspecific 183Get a thread-specific data value. 184.It Fn pthread_setspecific 185Set a thread-specific data value. 186.El 187.Ss Read/Write Lock Routines 188The functions available are as follows: 189.Pp 190.Bl -tag -width "pthread_rwlockattr_getpshared()" -compact 191.It Fn pthread_rwlock_init 192Initialise a read/write lock. 193.It Fn pthread_rwlock_destroy 194Destroy a read/write lock. 195.It Fn pthread_rwlock_rdlock 196Acquire a read/write lock for reading. 197.It Fn pthread_rwlock_timedrdlock 198Attempt to acquire a read/write lock for reading before a specific 199point in time. 200.It Fn pthread_rwlock_tryrdlock 201Attempt to acquire a read/write lock for reading without blocking. 202.It Fn pthread_rwlock_wrlock 203Acquire a read/write lock for writing. 204.It Fn pthread_rwlock_timedwrlock 205Attempt to acquire a read/write lock for writing before a specific 206point in time. 207.It Fn pthread_rwlock_trywrlock 208Attempt to acquire a read/write lock for writing without blocking. 209.It Fn pthread_rwlock_unlock 210Release a read/write lock. 211.It Fn pthread_rwlockattr_init 212Initialise a read/write lock. 213.It Fn pthread_rwlockattr_destroy 214Destroy a read/write lock. 215.It Fn pthread_rwlockattr_getpshared 216Get the process shared attribute. 217.It Fn pthread_rwlockattr_setpshared 218Set the process shared attribute. 219.El 220.Ss Thread Barrier Routines 221The functions available are as follows: 222.Pp 223.Bl -tag -width "pthread_barrierattr_getpshared()" -compact 224.It Fn pthread_barrier_init 225Initialize a barrier object. 226.It Fn pthread_barrier_destroy 227Destroy a barrier object. 228.It Fn pthread_barrier_wait 229Synchronize at a barrier. 230.It Fn pthread_barrierattr_init 231Initialize a barrier's attribute object. 232.It Fn pthread_barrierattr_destroy 233Destroy a barrier's attribute object. 234.It Fn pthread_barrierattr_getpshared 235Get the process-shared attribute of the barrier attribute's object. 236.It Fn pthread_barrierattr_setpshared 237Set the process-shared attribute of the barrier attribute's object. 238.El 239.Ss Thread Spinlock Routines 240The functions available are as follows: 241.Pp 242.Bl -tag -width "pthread_spin_trylock()" -compact 243.It Fn pthread_spin_init 244Initialize a spinlock object. 245.It Fn pthread_spin_destroy 246Destroy a spinlock object. 247.It Fn pthread_spin_lock 248Lock a spinlock object. 249.It Fn pthread_spin_trylock 250Attempt to lock a spinlock without blocking. 251.It Fn pthread_spin_unlock 252Unlock a spinlock object. 253.El 254.Ss Thread Routines 255The functions available are as follows: 256.Pp 257.Bl -tag -width "pthread_getconcurrency()" -compact 258.It Fn pthread_create 259Create a new thread. 260.It Fn pthread_cancel 261Cancel execution of a thread. 262.It Fn pthread_detach 263Detach a thread. 264.It Fn pthread_equal 265Compare thread IDs. 266.It Fn pthread_exit 267Terminate the calling thread. 268.It Fn pthread_getconcurrency 269Get level of concurrency. 270.It Fn pthread_setconcurrency 271Set level of concurrency. 272.It Fn pthread_join 273Wait for thread termination. 274.It Fn pthread_kill 275Send a signal to a specific thread. 276.It Fn pthread_once 277Dynamic package initialisation. 278.It Fn pthread_self 279Get the calling thread's ID. 280.It Fn pthread_setcancelstate 281Set cancelability state. 282.It Fn pthread_setcanceltype 283Set cancelability state. 284.It Fn pthread_testcancel 285Set cancelability state. 286.It Fn pthread_sigmask 287Examine/change a thread's signal mask. 288.El 289.Ss Thread states 290Threads can be in one of these states: 291.Pp 292.Bl -tag -offset indent -width Dv -compact 293.It cond_wait 294Executing 295.Xr pthread_cond_wait 3 296or 297.Xr pthread_cond_timedwait 3 . 298.It dead 299Waiting for resource deallocation by the thread garbage collector. 300.It deadlock 301Waiting for a resource held by the thread itself. 302.It fdlr_wait 303File descriptor read lock wait. 304.It fdlw_wait 305File descriptor write lock wait. 306.It fdr_wait 307Executing one of 308.Xr accept 2 , 309.Xr read 2 , 310.Xr readv 2 , 311.Xr recvfrom 2 , 312.Xr recvmsg 2 . 313.It fdw_wait 314Executing one of 315.Xr connect 2 , 316.Xr sendmsg 2 , 317.Xr sendto 2 , 318.Xr write 2 , 319.Xr writev 2 . 320.It file_wait 321Executing 322.Xr flockfile 3 323or similar. 324.It join 325Executing 326.Xr pthread_join 3 . 327.It mutex_wait 328Executing 329.Xr pthread_mutex_lock 3 . 330.It poll_wait 331Executing 332.Xr poll 2 . 333.It running 334Scheduled for, or engaged in, program execution. 335.It select_wait 336Executing 337.Xr select 2 . 338.It sigsuspend 339Executing 340.Xr sigsuspend 2 . 341.It sigwait 342Executing 343.Xr sigwait 3 . 344.It sleep_wait 345Executing 346.Xr sleep 3 347or 348.Xr nanosleep 2 . 349.It spinblock 350Waiting for a machine-level atomic lock. 351.It wait_wait 352Executing 353.Xr wait4 2 354or similar. 355.El 356.Ss Thread flags 357The first three flags are one of: 358.Pp 359.Bl -tag -offset indent -width 3en -compact 360.It "p" 361Private, system thread (e.g., the garbage collector). 362.It "E, C, or c" 363Thread is exiting (E), has a cancellation pending (C) (see 364.Xr pthread_cancel 3 ) , 365or is at a cancellation point (c). 366.It "t" 367Thread is being traced. 368.El 369.Pp 370The next 7 flags refer to thread attributes: 371.Pp 372.Bl -tag -offset indent -width 3en -compact 373.It "C" 374Thread is in the 375.Dv CONDQ . 376.It "R" 377Thread is in the 378.Dv WORKQ . 379.It "W" 380Thread is in the 381.Dv WAITQ . 382.It "P" 383Thread is in the 384.Dv PRIOQ . 385.It "d" 386Thread has been detached (see 387.Xr pthread_detach 3 ) . 388.It "i" 389Thread inherits scheduler properties. 390.It "f" 391Thread will save floating point context. 392.El 393.Ss Scheduling algorithm 394The scheduling algorithm used by the user-level thread library is 395roughly as follows: 396.Pp 397.Bl -enum -compact 398.It 399Threads each have a time slice credit which is debited 400by the actual time the thread spends in running. 401Freshly scheduled threads are given a time slice credit of 100000 usec. 402.It 403Give an incremental priority update to run-enabled threads that 404have not run since the last time that an incremental priority update 405was given to them. 406.It 407Choose the next run-enabled thread with the highest priority, 408that became inactive least recently, and has 409the largest remaining time slice. 410.El 411.Pp 412When all threads are blocked, the process also blocks. 413When there are no threads remaining, 414the process terminates with an exit code of zero. 415.Ss Thread stacks 416Each thread has (or should have) a different stack, whether it be provided by a 417user attribute, or provided automatically by the system. 418If a thread overflows its stack, unpredictable results may occur. 419System-allocated stacks (including that of the initial thread) 420are typically allocated in such a way that a 421.Dv SIGSEGV 422signal is delivered to the process when a stack overflows. 423.Pp 424Signals handlers are normally run on the stack of the currently executing 425thread. 426Hence, if you want to handle the 427.Dv SIGSEGV 428signal, you should make use of 429.Xr sigaltstack 2 430or 431.Xr sigprocmask 2 . 432.Ss Thread safety 433The following functions are not thread safe: 434.Bd -filled 435asctime(), 436basename(), 437catgets(), 438crypt(), 439ctime(), 440dbm_clearerr(), 441dbm_close(), 442dbm_delete(), 443dbm_error(), 444dbm_fetch(), 445dbm_firstkey(), 446dbm_nextkey(), 447dbm_open(), 448dbm_store(), 449dirname(), 450dlerror(), 451drand48(), 452ecvt(), 453encrypt(), 454endgrent(), 455endpwent(), 456fcvt(), 457ftw(), 458gcvt(), 459getc_unlocked(), 460getchar_unlocked(), 461getenv(), 462getgrent(), 463getgrgid(), 464getgrnam(), 465gethostbyaddr(), 466gethostbyname(), 467gethostent(), 468getlogin(), 469getnetbyaddr(), 470getnetbyname(), 471getnetent(), 472getopt(), 473getprotobyname(), 474getprotobynumber(), 475getprotoent(), 476getpwent(), 477getpwnam(), 478getpwuid(), 479getservbyname(), 480getservbyport(), 481getservent(), 482gmtime(), 483hcreate(), 484hdestroy(), 485hsearch(), 486inet_ntoa(), 487l64a(), 488lgamma(), 489lgammaf(), 490localeconv(), 491localtime(), 492lrand48(), 493mrand48(), 494nftw(), 495nl_langinfo(), 496putc_unlocked(), 497putchar_unlocked(), 498putenv(), 499rand(), 500readdir(), 501setenv(), 502setgrent(), 503setkey(), 504setpwent(), 505strerror(), 506strtok(), 507ttyname(), 508unsetenv(), 509.Ed 510.Pp 511The 512.Fn ctermid 513and 514.Fn tmpnam 515functions are not thread safe when passed a 516.Dv NULL 517argument. 518.Sh ENVIRONMENT 519.Bl -tag -width "LIBPTHREAD_DEBUG" 520.It Ev PTHREAD_DEBUG 521Enables verbose 522.Dv SIGINFO 523signal output. 524.It Ev LIBPTHREAD_DEBUG 525Display thread status every time the garbage collection thread runs, 526approximately once every 10 seconds. 527The status display verbosity is controlled by the 528.Ev PTHREAD_DEBUG 529environment variable. 530.El 531.Sh SEE ALSO 532.Xr pthread_atfork 3 , 533.Xr pthread_attr_init 3 , 534.Xr pthread_attr_setdetachstate 3 , 535.Xr pthread_attr_setguardsize 3 , 536.Xr pthread_attr_setstack 3 , 537.Xr pthread_attr_setstackaddr 3 , 538.Xr pthread_attr_setstacksize 3 , 539.Xr pthread_barrier_init 3 , 540.Xr pthread_barrier_wait 3 , 541.Xr pthread_barrierattr_getpshared 3 , 542.Xr pthread_barrierattr_init 3 , 543.Xr pthread_cancel 3 , 544.Xr pthread_cleanup_pop 3 , 545.Xr pthread_cleanup_push 3 , 546.Xr pthread_cond_broadcast 3 , 547.Xr pthread_cond_destroy 3 , 548.Xr pthread_cond_init 3 , 549.Xr pthread_cond_signal 3 , 550.Xr pthread_cond_timedwait 3 , 551.Xr pthread_cond_wait 3 , 552.Xr pthread_condattr_init 3 , 553.Xr pthread_create 3 , 554.Xr pthread_detach 3 , 555.Xr pthread_equal 3 , 556.Xr pthread_exit 3 , 557.Xr pthread_getspecific 3 , 558.Xr pthread_join 3 , 559.Xr pthread_key_create 3 , 560.Xr pthread_key_delete 3 , 561.Xr pthread_kill 3 , 562.Xr pthread_main_np 3 , 563.Xr pthread_mutex_destroy 3 , 564.Xr pthread_mutex_init 3 , 565.Xr pthread_mutex_lock 3 , 566.Xr pthread_mutex_unlock 3 , 567.Xr pthread_mutexattr 3 , 568.Xr pthread_once 3 , 569.Xr pthread_rwlock_destroy 3 , 570.Xr pthread_rwlock_init 3 , 571.Xr pthread_rwlock_rdlock 3 , 572.Xr pthread_rwlock_unlock 3 , 573.Xr pthread_rwlock_wrlock 3 , 574.Xr pthread_rwlockattr_destroy 3 , 575.Xr pthread_rwlockattr_getpshared 3 , 576.Xr pthread_rwlockattr_init 3 , 577.Xr pthread_rwlockattr_setpshared 3 , 578.Xr pthread_schedparam 3 , 579.Xr pthread_self 3 , 580.Xr pthread_set_name_np 3 , 581.Xr pthread_setspecific 3 , 582.Xr pthread_sigmask 3 , 583.Xr pthread_spin_init 3 , 584.Xr pthread_spin_lock 3 , 585.Xr pthread_spin_unlock 3 , 586.Xr pthread_stackseg_np 3 , 587.Xr pthread_testcancel 3 , 588.Xr pthread_yield 3 589.Sh STANDARDS 590The user-level thread library provides functions that 591conform to ISO/IEC 9945-1 ANSI/IEEE 592.Pq Dq Tn POSIX 593Std 1003.1 Second Edition 1996-07-12. 594.Sh AUTHORS 595John Birrell 596.Pa ( jb@freebsd.org ) 597wrote the majority of the user level thread library. 598.\" David Leonard did a fair bit too, but is far too modest. 599.Sh BUGS 600The library contains a scheduler that uses the 601process virtual interval timer to pre-empt running threads. 602This means that using 603.Xr setitimer 2 604to alter the process virtual timer will have undefined effects. 605The 606.Dv SIGVTALRM 607will never be delivered to threads in a process. 608.Pp 609Some pthread functions fail to work correctly when linked using the 610.Fl g 611option to 612.Xr cc 1 613or 614.Xr gcc 1 . 615The problems do not occur when linked using the 616.Fl ggdb 617option. 618