1 .\" 2 .\" Sun Microsystems, Inc. gratefully acknowledges The Open Group for 3 .\" permission to reproduce portions of its copyrighted documentation. 4 .\" Original documentation from The Open Group can be obtained online at 5 .\" http://www.opengroup.org/bookstore/. 6 .\" 7 .\" The Institute of Electrical and Electronics Engineers and The Open 8 .\" Group, have given us permission to reprint portions of their 9 .\" documentation. 10 .\" 11 .\" In the following statement, the phrase ``this text'' refers to portions 12 .\" of the system documentation. 13 .\" 14 .\" Portions of this text are reprinted and reproduced in electronic form 15 .\" in the SunOS Reference Manual, from IEEE Std 1003.1, 2004 Edition, 16 .\" Standard for Information Technology -- Portable Operating System 17 .\" Interface (POSIX), The Open Group Base Specifications Issue 6, 18 .\" Copyright (C) 2001-2004 by the Institute of Electrical and Electronics 19 .\" Engineers, Inc and The Open Group. In the event of any discrepancy 20 .\" between these versions and the original IEEE and The Open Group 21 .\" Standard, the original IEEE and The Open Group Standard is the referee 22 .\" document. The original Standard can be obtained online at 23 .\" http://www.opengroup.org/unix/online.html. 24 .\" 25 .\" This notice shall appear on any product containing this material. 26 .\" 27 .\" The contents of this file are subject to the terms of the 28 .\" Common Development and Distribution License (the "License"). 29 .\" You may not use this file except in compliance with the License. 30 .\" 31 .\" You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 32 .\" or http://www.opensolaris.org/os/licensing. 33 .\" See the License for the specific language governing permissions 34 .\" and limitations under the License. 35 .\" 36 .\" When distributing Covered Code, include this CDDL HEADER in each 37 .\" file and include the License file at usr/src/OPENSOLARIS.LICENSE. 38 .\" If applicable, add the following below this CDDL HEADER, with the 39 .\" fields enclosed by brackets "[]" replaced with your own identifying 40 .\" information: Portions Copyright [yyyy] [name of copyright owner] 41 .\" 42 .\" 43 .\" Portions Copyright (c) 1995 IEEE All Rights Reserved 44 .\" Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved. 45 .\" Copyright (c) 2001, The IEEE and The Open Group. All Rights Reserved. 46 .\" 47 .TH CONDITION 5 "May 16, 2020" 48 .SH NAME 49 condition \- concepts related to condition variables 50 .SH DESCRIPTION 51 Occasionally, a thread running within a mutex needs to wait for an event, in 52 which case it blocks or sleeps. When a thread is waiting for another thread to 53 communicate its disposition, it uses a condition variable in conjunction with a 54 mutex. Although a mutex is exclusive and the code it protects is sharable (at 55 certain moments), condition variables enable the synchronization of differing 56 events that share a mutex, but not necessarily data. Several condition 57 variables may be used by threads to signal each other when a task is complete, 58 which then allows the next waiting thread to take ownership of the mutex. 59 .sp 60 .LP 61 A condition variable enables threads to atomically block and test the condition 62 under the protection of a mutual exclusion lock (mutex) until the condition is 63 satisfied. If the condition is false, a thread blocks on a condition variable 64 and atomically releases the mutex that is waiting for the condition to change. 65 If another thread changes the condition, it may wake up waiting threads by 66 signaling the associated condition variable. The waiting threads, upon 67 awakening, reacquire the mutex and re-evaluate the condition. 68 .SS "Initialize" 69 Condition variables and mutexes should be global. Condition variables that are 70 allocated in writable memory can synchronize threads among processes if they 71 are shared by the cooperating processes (see \fBmmap\fR(2)) and are initialized 72 for this purpose. 73 .sp 74 .LP 75 The scope of a condition variable is either intra-process or inter-process. 76 This is dependent upon whether the argument is passed implicitly or explicitly 77 to the initialization of that condition variable. A condition variable does 78 not need to be explicitly initialized. A condition variable is initialized with 79 all zeros, by default, and its scope is set to within the calling process. For 80 inter-process synchronization, a condition variable must be initialized once, 81 and only once, before use. 82 .sp 83 .LP 84 A condition variable must not be simultaneously initialized by multiple threads 85 or re-initialized while in use by other threads. 86 .sp 87 .LP 88 Condition variables attributes may be set to the default or customized at 89 initialization. POSIX threads even allow the default values to be customized. 90 Establishing these attributes varies depending upon whether POSIX or Solaris 91 threads are used. Similar to the distinctions between POSIX and Solaris thread 92 creation, POSIX condition variables implement the default, intra-process, 93 unless an attribute object is modified for inter-process prior to the 94 initialization of the condition variable. Solaris condition variables also 95 implement as the default, intra-process; however, they set this attribute 96 according to the argument, \fItype\fR, passed to their initialization function. 97 .SS "Condition Wait" 98 The condition wait interface allows a thread to wait for a condition and 99 atomically release the associated mutex that it needs to hold to check the 100 condition. The thread waits for another thread to make the condition true and 101 that thread's resulting call to signal and wakeup the waiting thread. 102 .SS "Condition Signaling" 103 A condition signal allows a thread to unblock the next thread waiting on the 104 condition variable, whereas, a condition broadcast allows a thread to unblock 105 all threads waiting on the condition variable. 106 .SS "Destroy" 107 The condition destroy functions destroy any state, but not the space, 108 associated with the condition variable. 109 .SH ATTRIBUTES 110 See \fBattributes\fR(5) for descriptions of the following attributes: 111 .sp 112 113 .sp 114 .TS 115 box; 116 c | c 117 l | l . 118 ATTRIBUTE TYPE ATTRIBUTE VALUE 119 _ 120 MT-Level MT-Safe 121 .TE 122 123 .SH SEE ALSO 124 \fBfork\fR(2), \fBmmap\fR(2), \fBsetitimer\fR(2), \fBshmop\fR(2), 125 \fBcond_broadcast\fR(3C), \fBcond_destroy\fR(3C), \fBcond_init\fR(3C), 126 \fBcond_signal\fR(3C), \fBcond_timedwait\fR(3C), \fBcond_wait\fR(3C), 127 \fBpthread_cond_broadcast\fR(3C), \fBpthread_cond_destroy\fR(3C), 128 \fBpthread_cond_init\fR(3C), \fBpthread_cond_signal\fR(3C), 129 \fBpthread_cond_timedwait\fR(3C), \fBpthread_cond_wait\fR(3C), 130 \fBpthread_condattr_init\fR(3C), \fBsignal\fR(3C), \fBattributes\fR(5), 131 \fBmutex\fR(5), \fBstandards\fR(5) 132 .SH NOTES 133 If more than one thread is blocked on a condition variable, the order in which 134 threads are unblocked is determined by the scheduling policy. 135 .sp 136 .LP 137 \fBUSYNC_THREAD\fR does not support multiple mappings to the same logical 138 synch object. If you need to \fBmmap()\fR a synch object to different locations 139 within the same address space, then the synch object should be initialized as a 140 shared object \fBUSYNC_PROCESS\fR for Solaris, and 141 \fBPTHREAD_PROCESS_PRIVATE\fR for POSIX.