Print this page
11622 clean up rarer mandoc lint warnings
   1 '\" te
   2 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
   3 .\" Copyright 2016 Joyent, Inc.
   4 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
   5 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
   6 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
   7 .TH THREADS 5 "Mar 27, 2016"
   8 .SH NAME
   9 threads, pthreads \- POSIX pthreads, c11, and illumos threads concepts
  10 .SH SYNOPSIS
  11 .SS "POSIX"
  12 .LP
  13 .nf
  14 gcc -D_REENTRANT [ \fIflag\fR... ] \fIfile\fR... [ \fIlibrary\fR... ]
  15 .fi
  16 
  17 .LP
  18 .nf
  19 #include <pthread.h>
  20 .fi
  21 
  22 .SS "C11"
  23 .LP
  24 .nf
  25 gcc -std=c11 -D_REENTRANT [ \fIflag\fR... ] \fIfile\fR... [ \fIlibrary\fR... ]
  26 .fi
  27 
  28 .LP
  29 .nf
  30 #include <threads.h>
  31 .fi
  32 
  33 .SS "illumos"
  34 .LP
  35 .nf
  36 gcc -D_REENTRANT [ \fIflag\fR... ] \fIfile\fR... [ \fIlibrary\fR... ]
  37 .fi
  38 
  39 .LP
  40 .nf
  41 #include <sched.h>
  42 .fi
  43 
  44 .LP
  45 .nf
  46 #include <thread.h>
  47 .fi
  48 
  49 .SH DESCRIPTION
  50 .LP
  51 A thread is an independent source of execution within a process. Every process
  52 is created with a single thread, which calls the
  53 .B main
  54 function. A process may have multiple threads, all of which are scheduled
  55 independently by the system and may run concurrently. Threads within a process
  56 all use the same address space and as a result can access all data in the
  57 process; however, each thread is created with its own attributes and its own
  58 stack. When a thread is created, it inherits the signal mask of the thread which
  59 created it, but it has no pending signals.
  60 .sp
  61 .LP
  62 All threads of execution have their own, independent life time, though it is
  63 ultimately bounded by the life time of the process. If the process terminates
  64 for any reason, whether due to a call to \fBexit\fR(3C), the receipt of a fatal
  65 signal, or some other reason, then all threads within the process are
  66 terminated. Threads may themselves exit and status information of them may be
  67 obtained, for more information, see the \fBpthread_detach\fR(3C),
  68 \fBpthread_join\fR(3C), and \fBpthread_exit\fR(3C) functions, and their
  69 equivalents as described in the tables later on in the manual.
  70 .sp
  71 .LP
  72 Most hardware platforms do not have any special synchronization for data objects
  73 which may be accessed concurrently from multiple threads of execution. To avoid
  74 such problems, programs may use atomic operations (see \fBatomic_ops\fR(3C)) and
  75 locking primitives, such as mutexes, readers/writer locks, condition variables,
  76 and semaphores. Note, that depending on the hardware platform, memory
  77 synchronization may be necessary, for more information, see \fBmembar_ops\fR(3C).
  78 .LP
  79 POSIX, C11, and illumos threads each have their own implementation within
  80 \fBlibc\fR(3LIB). All implementations are interoperable, their functionality
  81 similar, and can be used within the same application. Only POSIX threads are
  82 guaranteed to be fully portable to other POSIX-compliant environments. C11
  83 threads are an optional part of ISO C11 and may not exist on every ISO C11
  84 platform. POSIX, C11, and illumos threads require different source and include
  85 files. See \fBSYNOPSIS\fR.
  86 .SS "Similarities"
  87 .LP
  88 Most of the POSIX and illumos threading functions have counterparts with each
  89 other. POSIX function names, with the exception of the semaphore names, have a
  90 "\fBpthread\fR" prefix. Function names for similar POSIX and illumos functions
  91 have similar endings. Typically, similar POSIX and illumos functions have the
  92 same number and use of arguments.
  93 .SS "Differences"
  94 .LP
  95 POSIX pthreads and illumos threads differ in the following ways:
  96 .RS +4
  97 .TP
  98 .ie t \(bu
  99 .el o
 100 POSIX threads are more portable.
 101 .RE
 102 .RS +4
 103 .TP
 104 .ie t \(bu
 105 .el o
 106 POSIX threads establish characteristics  for each thread according to
 107 configurable attribute objects.
 108 .RE
 109 .RS +4
 110 .TP
 111 .ie t \(bu
 112 .el o
 113 POSIX pthreads implement thread cancellation.
 114 .RE


 121 .RS +4
 122 .TP
 123 .ie t \(bu
 124 .el o
 125 POSIX pthreads allow for clean-up handlers for \fBfork\fR(2) calls.
 126 .RE
 127 .RS +4
 128 .TP
 129 .ie t \(bu
 130 .el o
 131 illumos threads can be suspended and continued.
 132 .RE
 133 .RS +4
 134 .TP
 135 .ie t \(bu
 136 .el o
 137 illumos threads implement daemon threads, for whose demise the process does not
 138 wait.
 139 .RE
 140 .SS "Comparison to C11 Threads"
 141 .LP
 142 C11 threads are not as functional as either POSIX or illumos threads. C11
 143 threads only support intra-process locking and do not have any form of
 144 readers/writer locking or semaphores. In general, POSIX threads will be more
 145 portable than C11 threads, all POSIX-compliant systems support pthreads;
 146 however, not all C environments support C11 Threads.
 147 .sp
 148 .LP
 149 In addition to lacking other common synchronization primitives, the ISO/IEC
 150 standard for C11 threads does not have rich error semantics. In an effort to not
 151 extend the set of error numbers standardized in ISO/IEC C11, none of the
 152 routines set errno and instead multiple distinguishable errors, aside from the
 153 equivalent to ENOMEM and EBUSY, are all squashed into one. As such, users of the
 154 platform are encouraged to use POSIX threads, unless a portability concern
 155 dictates otherwise.
 156 
 157 .SH FUNCTION COMPARISON
 158 .LP
 159 The following table compares the POSIX pthreads, C11 threads, and illumos
 160 threads functions.  When a comparable interface is not available either in POSIX
 161 pthreads, C11 threads  or illumos threads, a hyphen (\fB-\fR) appears in the
 162 column.
 163 .SS "Functions Related to Creation"
 164 
 165 .TS
 166 l l l
 167 l l l .
 168 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 169 \fBpthread_create()\fR  \fBthr_create()\fR      \fBthrd_create()\fR
 170 \fBpthread_attr_init()\fR       \fB-\fR \fB-\fR
 171 \fBpthread_attr_setdetachstate()\fR     \fB-\fR \fB-\fR
 172 \fBpthread_attr_getdetachstate()\fR     \fB-\fR \fB-\fR
 173 \fBpthread_attr_setinheritsched()\fR    \fB-\fR \fB-\fR
 174 \fBpthread_attr_getinheritsched()\fR    \fB-\fR \fB-\fR
 175 \fBpthread_attr_setschedparam()\fR      \fB-\fR \fB-\fR
 176 \fBpthread_attr_getschedparam()\fR      \fB-\fR \fB-\fR
 177 \fBpthread_attr_setschedpolicy()\fR     \fB-\fR \fB-\fR
 178 \fBpthread_attr_getschedpolicy()\fR     \fB-\fR \fB-\fR


 319 \fBpthread_rwlock_tryrdlock()\fR        \fBrw_tryrdlock()\fR    \fB-\fR
 320 \fBpthread_rwlock_wrlock()\fR   \fBrw_wrlock()\fR       \fB-\fR
 321 \fBpthread_rwlock_trywrlock()\fR        \fBrw_trywrlock()\fR    \fB-\fR
 322 \fBpthread_rwlock_unlock()\fR   \fBrw_unlock()\fR       \fB-\fR
 323 \fBpthread_rwlock_destroy()\fR  \fBrwlock_destroy()\fR  \fB-\fR
 324 \fBpthread_rwlockattr_init()\fR \fB-\fR \fB-\fR
 325 \fBpthread_rwlockattr_destroy()\fR      \fB-\fR \fB-\fR
 326 \fBpthread_rwlockattr_getpshared()\fR   \fB-\fR \fB-\fR
 327 \fBpthread_rwlockattr_setpshared()\fR   \fB-\fR \fB-\fR
 328 .TE
 329 
 330 .SS "Functions Related to Semaphores"
 331 
 332 .TS
 333 l l l
 334 l l l .
 335 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 336 \fBsem_init()\fR        \fBsema_init()\fR       \fB-\fR
 337 \fBsem_open()\fR        \fB-\fR \fB-\fR
 338 \fBsem_close()\fR       \fB-\fR \fB-\fR
 339 \fBsem_wait()\fR        \fBsema_wait()\ \fB-\fR
 340 \fBsem_trywait()\fR     \fBsema_trywait()\fR    \fB-\fR
 341 \fBsem_post()\fR        \fBsema_post()\fR       \fB-\fR
 342 \fBsem_getvalue()\fR    \fB-\fR \fB-\fR
 343 \fBsem_unlink()\fR      \fB-\fR \fB-\fR
 344 \fBsem_destroy()\fR     \fBsema_destroy()\fR    \fB-\fR
 345 .TE
 346 
 347 .SS "Functions Related to fork(\|) Clean Up"
 348 
 349 .TS
 350 l l l
 351 l l l .
 352 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 353 \fBpthread_atfork()\fR  \fB-\fR \fB-\fR
 354 .TE
 355 
 356 .SS "Functions Related to Limits"
 357 
 358 .TS
 359 l l l
 360 l l l .
 361 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 362 \fBpthread_once()\fR    \fB-\fR \fBcall_once()\fR
 363 .TE
 364 
 365 .SS "Functions Related to Debugging"
 366 
 367 .TS
 368 l l l
 369 l l l .
 370 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 371 \fB-\fR \fBthr_stksegment()\fR  \fB-\fR
 372 .TE
 373 
 374 .SH LOCKING
 375 .SS "Synchronization"
 376 .LP
 377 Multithreaded behavior is asynchronous, and therefore,  optimized for
 378 concurrent and parallel processing. As threads, always from within the same
 379 process and  sometimes from multiple processes, share global data with each
 380 other, they are not guaranteed exclusive access to the shared data at any point
 381 in time. Securing mutually exclusive access to shared data requires
 382 synchronization among the threads. Both POSIX and illumos implement four
 383 synchronization mechanisms: mutexes, condition variables, reader/writer locking
 384 (\fIoptimized frequent-read occasional-write mutex\fR), and semaphores, where as
 385 C11 threads only implement two mechanisms: mutexes and condition variables.
 386 .sp
 387 .LP
 388 Synchronizing multiple threads diminishes their concurrency. The coarser the
 389 grain of synchronization, that is, the larger the block of code that is locked,
 390 the lesser the concurrency.
 391 .SS "MT \fBfork()\fR"
 392 .LP
 393 If a threads program calls \fBfork\fR(2), it implicitly calls \fBfork1\fR(2),
 394 which replicates only the calling thread. Should there be any outstanding
 395 mutexes throughout the process, the application should call
 396 \fBpthread_atfork\fR(3C) to wait for and acquire those mutexes prior to calling
 397 \fBfork()\fR.
 398 .SH SCHEDULING
 399 .SS "POSIX Threads"
 400 .LP
 401 illumos supports the following three POSIX scheduling policies:
 402 .sp
 403 .ne 2
 404 .na
 405 \fB\fBSCHED_OTHER\fR\fR
 406 .ad
 407 .RS 15n
 408 Traditional Timesharing scheduling policy. It is based on the timesharing (TS)
 409 scheduling class.
 410 .RE
 411 
 412 .sp
 413 .ne 2
 414 .na
 415 \fB\fBSCHED_FIFO\fR\fR
 416 .ad
 417 .RS 15n
 418 First-In-First-Out scheduling policy. Threads scheduled to this policy, if not
 419 preempted by a higher priority, will proceed until completion. Such threads are
 420 in real-time (RT) scheduling class. The calling process must have a effective


 451 .ne 2
 452 .na
 453 \fB\fBSCHED_FSS\fR\fR
 454 .ad
 455 .RS 13n
 456 Threads are scheduled according to the Fair-Share Class (FSS) policy as
 457 described in \fBpriocntl\fR(2).
 458 .RE
 459 
 460 .sp
 461 .ne 2
 462 .na
 463 \fB\fBSCHED_FX\fR\fR
 464 .ad
 465 .RS 13n
 466 Threads are scheduled according to the Fixed-Priority Class (FX) policy as
 467 described in \fBpriocntl\fR(2).
 468 .RE
 469 
 470 .SS "illumos Threads"
 471 .LP
 472 Only scheduling policy supported is \fBSCHED_OTHER\fR, which is timesharing,
 473 based on the \fBTS\fR scheduling class.
 474 .SH ERRORS
 475 .LP
 476 In a multithreaded application, \fBEINTR\fR can be returned from blocking
 477 system calls when another thread calls \fBforkall\fR(2).
 478 .SH USAGE
 479 .SS "\fB-mt\fR compiler option"
 480 .LP
 481 The \fB-mt\fR compiler option compiles and links for multithreaded code. It
 482 compiles source files with \(mi\fBD_REENTRANT\fR and augments the set of
 483 support libraries properly.
 484 .sp
 485 .LP
 486 Users of other compilers such as gcc and clang should manually set
 487 \(mi\fBD_REENTRANT\fR on the compilation line. There are no other libraries or
 488 flags necessary.
 489 .SH ATTRIBUTES
 490 .LP
 491 See \fBattributes\fR(5) for descriptions of the following attributes:
 492 .sp
 493 
 494 .sp
 495 .TS
 496 box;
 497 c | c
 498 l | l .
 499 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 500 _
 501 MT-Level        MT-Safe, Fork 1-Safe
 502 .TE
 503 
 504 .SH SEE ALSO
 505 .LP
 506 \fBcrle\fR(1), \fBfork\fR(2), \fBpriocntl\fR(2), \fBlibpthread\fR(3LIB),
 507 \fBlibrt\fR(3LIB), \fBlibthread\fR(3LIB), \fBpthread_atfork\fR(3C),
 508 \fBpthread_create\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)
 509 .sp
 510 .LP
 511 \fILinker and Libraries Guide\fR
   1 '\" te
   2 .\" Copyright (c) 2008, Sun Microsystems, Inc.  All Rights Reserved.
   3 .\" Copyright 2016 Joyent, Inc.
   4 .\" The contents of this file are subject to the terms of the Common Development and Distribution License (the "License").  You may not use this file except in compliance with the License.
   5 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE or http://www.opensolaris.org/os/licensing.  See the License for the specific language governing permissions and limitations under the License.
   6 .\" When distributing Covered Code, include this CDDL HEADER in each file and include the License file at usr/src/OPENSOLARIS.LICENSE.  If applicable, add the following below this CDDL HEADER, with the fields enclosed by brackets "[]" replaced with your own identifying information: Portions Copyright [yyyy] [name of copyright owner]
   7 .TH THREADS 5 "Mar 27, 2016"
   8 .SH NAME
   9 threads, pthreads \- POSIX pthreads, c11, and illumos threads concepts
  10 .SH SYNOPSIS
  11 .SS "POSIX"

  12 .nf
  13 gcc -D_REENTRANT [ \fIflag\fR... ] \fIfile\fR... [ \fIlibrary\fR... ]
  14 .fi
  15 
  16 .LP
  17 .nf
  18 #include <pthread.h>
  19 .fi
  20 
  21 .SS "C11"

  22 .nf
  23 gcc -std=c11 -D_REENTRANT [ \fIflag\fR... ] \fIfile\fR... [ \fIlibrary\fR... ]
  24 .fi
  25 
  26 .LP
  27 .nf
  28 #include <threads.h>
  29 .fi
  30 
  31 .SS "illumos"

  32 .nf
  33 gcc -D_REENTRANT [ \fIflag\fR... ] \fIfile\fR... [ \fIlibrary\fR... ]
  34 .fi
  35 
  36 .LP
  37 .nf
  38 #include <sched.h>
  39 .fi
  40 
  41 .LP
  42 .nf
  43 #include <thread.h>
  44 .fi
  45 
  46 .SH DESCRIPTION

  47 A thread is an independent source of execution within a process. Every process
  48 is created with a single thread, which calls the
  49 .B main
  50 function. A process may have multiple threads, all of which are scheduled
  51 independently by the system and may run concurrently. Threads within a process
  52 all use the same address space and as a result can access all data in the
  53 process; however, each thread is created with its own attributes and its own
  54 stack. When a thread is created, it inherits the signal mask of the thread which
  55 created it, but it has no pending signals.
  56 .sp
  57 .LP
  58 All threads of execution have their own, independent life time, though it is
  59 ultimately bounded by the life time of the process. If the process terminates
  60 for any reason, whether due to a call to \fBexit\fR(3C), the receipt of a fatal
  61 signal, or some other reason, then all threads within the process are
  62 terminated. Threads may themselves exit and status information of them may be
  63 obtained, for more information, see the \fBpthread_detach\fR(3C),
  64 \fBpthread_join\fR(3C), and \fBpthread_exit\fR(3C) functions, and their
  65 equivalents as described in the tables later on in the manual.
  66 .sp
  67 .LP
  68 Most hardware platforms do not have any special synchronization for data objects
  69 which may be accessed concurrently from multiple threads of execution. To avoid
  70 such problems, programs may use atomic operations (see \fBatomic_ops\fR(3C)) and
  71 locking primitives, such as mutexes, readers/writer locks, condition variables,
  72 and semaphores. Note, that depending on the hardware platform, memory
  73 synchronization may be necessary, for more information, see \fBmembar_ops\fR(3C).
  74 .LP
  75 POSIX, C11, and illumos threads each have their own implementation within
  76 \fBlibc\fR(3LIB). All implementations are interoperable, their functionality
  77 similar, and can be used within the same application. Only POSIX threads are
  78 guaranteed to be fully portable to other POSIX-compliant environments. C11
  79 threads are an optional part of ISO C11 and may not exist on every ISO C11
  80 platform. POSIX, C11, and illumos threads require different source and include
  81 files. See \fBSYNOPSIS\fR.
  82 .SS "Similarities"

  83 Most of the POSIX and illumos threading functions have counterparts with each
  84 other. POSIX function names, with the exception of the semaphore names, have a
  85 "\fBpthread\fR" prefix. Function names for similar POSIX and illumos functions
  86 have similar endings. Typically, similar POSIX and illumos functions have the
  87 same number and use of arguments.
  88 .SS "Differences"

  89 POSIX pthreads and illumos threads differ in the following ways:
  90 .RS +4
  91 .TP
  92 .ie t \(bu
  93 .el o
  94 POSIX threads are more portable.
  95 .RE
  96 .RS +4
  97 .TP
  98 .ie t \(bu
  99 .el o
 100 POSIX threads establish characteristics  for each thread according to
 101 configurable attribute objects.
 102 .RE
 103 .RS +4
 104 .TP
 105 .ie t \(bu
 106 .el o
 107 POSIX pthreads implement thread cancellation.
 108 .RE


 115 .RS +4
 116 .TP
 117 .ie t \(bu
 118 .el o
 119 POSIX pthreads allow for clean-up handlers for \fBfork\fR(2) calls.
 120 .RE
 121 .RS +4
 122 .TP
 123 .ie t \(bu
 124 .el o
 125 illumos threads can be suspended and continued.
 126 .RE
 127 .RS +4
 128 .TP
 129 .ie t \(bu
 130 .el o
 131 illumos threads implement daemon threads, for whose demise the process does not
 132 wait.
 133 .RE
 134 .SS "Comparison to C11 Threads"

 135 C11 threads are not as functional as either POSIX or illumos threads. C11
 136 threads only support intra-process locking and do not have any form of
 137 readers/writer locking or semaphores. In general, POSIX threads will be more
 138 portable than C11 threads, all POSIX-compliant systems support pthreads;
 139 however, not all C environments support C11 Threads.
 140 .sp
 141 .LP
 142 In addition to lacking other common synchronization primitives, the ISO/IEC
 143 standard for C11 threads does not have rich error semantics. In an effort to not
 144 extend the set of error numbers standardized in ISO/IEC C11, none of the
 145 routines set errno and instead multiple distinguishable errors, aside from the
 146 equivalent to ENOMEM and EBUSY, are all squashed into one. As such, users of the
 147 platform are encouraged to use POSIX threads, unless a portability concern
 148 dictates otherwise.
 149 
 150 .SH FUNCTION COMPARISON

 151 The following table compares the POSIX pthreads, C11 threads, and illumos
 152 threads functions.  When a comparable interface is not available either in POSIX
 153 pthreads, C11 threads  or illumos threads, a hyphen (\fB-\fR) appears in the
 154 column.
 155 .SS "Functions Related to Creation"
 156 
 157 .TS
 158 l l l
 159 l l l .
 160 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 161 \fBpthread_create()\fR  \fBthr_create()\fR      \fBthrd_create()\fR
 162 \fBpthread_attr_init()\fR       \fB-\fR \fB-\fR
 163 \fBpthread_attr_setdetachstate()\fR     \fB-\fR \fB-\fR
 164 \fBpthread_attr_getdetachstate()\fR     \fB-\fR \fB-\fR
 165 \fBpthread_attr_setinheritsched()\fR    \fB-\fR \fB-\fR
 166 \fBpthread_attr_getinheritsched()\fR    \fB-\fR \fB-\fR
 167 \fBpthread_attr_setschedparam()\fR      \fB-\fR \fB-\fR
 168 \fBpthread_attr_getschedparam()\fR      \fB-\fR \fB-\fR
 169 \fBpthread_attr_setschedpolicy()\fR     \fB-\fR \fB-\fR
 170 \fBpthread_attr_getschedpolicy()\fR     \fB-\fR \fB-\fR


 311 \fBpthread_rwlock_tryrdlock()\fR        \fBrw_tryrdlock()\fR    \fB-\fR
 312 \fBpthread_rwlock_wrlock()\fR   \fBrw_wrlock()\fR       \fB-\fR
 313 \fBpthread_rwlock_trywrlock()\fR        \fBrw_trywrlock()\fR    \fB-\fR
 314 \fBpthread_rwlock_unlock()\fR   \fBrw_unlock()\fR       \fB-\fR
 315 \fBpthread_rwlock_destroy()\fR  \fBrwlock_destroy()\fR  \fB-\fR
 316 \fBpthread_rwlockattr_init()\fR \fB-\fR \fB-\fR
 317 \fBpthread_rwlockattr_destroy()\fR      \fB-\fR \fB-\fR
 318 \fBpthread_rwlockattr_getpshared()\fR   \fB-\fR \fB-\fR
 319 \fBpthread_rwlockattr_setpshared()\fR   \fB-\fR \fB-\fR
 320 .TE
 321 
 322 .SS "Functions Related to Semaphores"
 323 
 324 .TS
 325 l l l
 326 l l l .
 327 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 328 \fBsem_init()\fR        \fBsema_init()\fR       \fB-\fR
 329 \fBsem_open()\fR        \fB-\fR \fB-\fR
 330 \fBsem_close()\fR       \fB-\fR \fB-\fR
 331 \fBsem_wait()\fR        \fBsema_wait()\fR       \fB-\fR
 332 \fBsem_trywait()\fR     \fBsema_trywait()\fR    \fB-\fR
 333 \fBsem_post()\fR        \fBsema_post()\fR       \fB-\fR
 334 \fBsem_getvalue()\fR    \fB-\fR \fB-\fR
 335 \fBsem_unlink()\fR      \fB-\fR \fB-\fR
 336 \fBsem_destroy()\fR     \fBsema_destroy()\fR    \fB-\fR
 337 .TE
 338 
 339 .SS "Functions Related to fork(\|) Clean Up"
 340 
 341 .TS
 342 l l l
 343 l l l .
 344 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 345 \fBpthread_atfork()\fR  \fB-\fR \fB-\fR
 346 .TE
 347 
 348 .SS "Functions Related to Limits"
 349 
 350 .TS
 351 l l l
 352 l l l .
 353 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 354 \fBpthread_once()\fR    \fB-\fR \fBcall_once()\fR
 355 .TE
 356 
 357 .SS "Functions Related to Debugging"
 358 
 359 .TS
 360 l l l
 361 l l l .
 362 \fBPOSIX\fR     \fBillumos\fR   \fBC11\fR
 363 \fB-\fR \fBthr_stksegment()\fR  \fB-\fR
 364 .TE
 365 
 366 .SH LOCKING
 367 .SS "Synchronization"

 368 Multithreaded behavior is asynchronous, and therefore,  optimized for
 369 concurrent and parallel processing. As threads, always from within the same
 370 process and  sometimes from multiple processes, share global data with each
 371 other, they are not guaranteed exclusive access to the shared data at any point
 372 in time. Securing mutually exclusive access to shared data requires
 373 synchronization among the threads. Both POSIX and illumos implement four
 374 synchronization mechanisms: mutexes, condition variables, reader/writer locking
 375 (\fIoptimized frequent-read occasional-write mutex\fR), and semaphores, where as
 376 C11 threads only implement two mechanisms: mutexes and condition variables.
 377 .sp
 378 .LP
 379 Synchronizing multiple threads diminishes their concurrency. The coarser the
 380 grain of synchronization, that is, the larger the block of code that is locked,
 381 the lesser the concurrency.
 382 .SS "MT \fBfork()\fR"

 383 If a threads program calls \fBfork\fR(2), it implicitly calls \fBfork1\fR(2),
 384 which replicates only the calling thread. Should there be any outstanding
 385 mutexes throughout the process, the application should call
 386 \fBpthread_atfork\fR(3C) to wait for and acquire those mutexes prior to calling
 387 \fBfork()\fR.
 388 .SH SCHEDULING
 389 .SS "POSIX Threads"

 390 illumos supports the following three POSIX scheduling policies:
 391 .sp
 392 .ne 2
 393 .na
 394 \fB\fBSCHED_OTHER\fR\fR
 395 .ad
 396 .RS 15n
 397 Traditional Timesharing scheduling policy. It is based on the timesharing (TS)
 398 scheduling class.
 399 .RE
 400 
 401 .sp
 402 .ne 2
 403 .na
 404 \fB\fBSCHED_FIFO\fR\fR
 405 .ad
 406 .RS 15n
 407 First-In-First-Out scheduling policy. Threads scheduled to this policy, if not
 408 preempted by a higher priority, will proceed until completion. Such threads are
 409 in real-time (RT) scheduling class. The calling process must have a effective


 440 .ne 2
 441 .na
 442 \fB\fBSCHED_FSS\fR\fR
 443 .ad
 444 .RS 13n
 445 Threads are scheduled according to the Fair-Share Class (FSS) policy as
 446 described in \fBpriocntl\fR(2).
 447 .RE
 448 
 449 .sp
 450 .ne 2
 451 .na
 452 \fB\fBSCHED_FX\fR\fR
 453 .ad
 454 .RS 13n
 455 Threads are scheduled according to the Fixed-Priority Class (FX) policy as
 456 described in \fBpriocntl\fR(2).
 457 .RE
 458 
 459 .SS "illumos Threads"

 460 Only scheduling policy supported is \fBSCHED_OTHER\fR, which is timesharing,
 461 based on the \fBTS\fR scheduling class.
 462 .SH ERRORS

 463 In a multithreaded application, \fBEINTR\fR can be returned from blocking
 464 system calls when another thread calls \fBforkall\fR(2).
 465 .SH USAGE
 466 .SS "\fB-mt\fR compiler option"

 467 The \fB-mt\fR compiler option compiles and links for multithreaded code. It
 468 compiles source files with \(mi\fBD_REENTRANT\fR and augments the set of
 469 support libraries properly.
 470 .sp
 471 .LP
 472 Users of other compilers such as gcc and clang should manually set
 473 \(mi\fBD_REENTRANT\fR on the compilation line. There are no other libraries or
 474 flags necessary.
 475 .SH ATTRIBUTES

 476 See \fBattributes\fR(5) for descriptions of the following attributes:
 477 .sp
 478 
 479 .sp
 480 .TS
 481 box;
 482 c | c
 483 l | l .
 484 ATTRIBUTE TYPE  ATTRIBUTE VALUE
 485 _
 486 MT-Level        MT-Safe, Fork 1-Safe
 487 .TE
 488 
 489 .SH SEE ALSO

 490 \fBcrle\fR(1), \fBfork\fR(2), \fBpriocntl\fR(2), \fBlibpthread\fR(3LIB),
 491 \fBlibrt\fR(3LIB), \fBlibthread\fR(3LIB), \fBpthread_atfork\fR(3C),
 492 \fBpthread_create\fR(3C), \fBattributes\fR(5), \fBstandards\fR(5)
 493 .sp
 494 .LP
 495 \fILinker and Libraries Guide\fR