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 "Jul 20, 1998" 48 .SH NAME 49 condition \- concepts related to condition variables 50 .SH DESCRIPTION 51 .sp 52 .LP 53 Occasionally, a thread running within a mutex needs to wait for an event, in 54 which case it blocks or sleeps. When a thread is waiting for another thread to 55 communicate its disposition, it uses a condition variable in conjunction with a 56 mutex. Although a mutex is exclusive and the code it protects is sharable (at 57 certain moments), condition variables enable the synchronization of differing 58 events that share a mutex, but not necessarily data. Several condition 59 variables may be used by threads to signal each other when a task is complete, 60 which then allows the next waiting thread to take ownership of the mutex. 61 .sp 62 .LP 63 A condition variable enables threads to atomically block and test the condition 64 under the protection of a mutual exclusion lock (mutex) until the condition is 65 satisfied. If the condition is false, a thread blocks on a condition variable 66 and atomically releases the mutex that is waiting for the condition to change. 67 If another thread changes the condition, it may wake up waiting threads by 68 signaling the associated condition variable. The waiting threads, upon 69 awakening, reacquire the mutex and re-evaluate the condition. 70 .SS "Initialize" 71 .sp 72 .LP 73 Condition variables and mutexes should be global. Condition variables that are 74 allocated in writable memory can synchronize threads among processes if they 75 are shared by the cooperating processes (see \fBmmap\fR(2)) and are initialized 76 for this purpose. 77 .sp 78 .LP 79 The scope of a condition variable is either intra-process or inter-process. 80 This is dependent upon whether the argument is passed implicitly or explicitly 81 to the initialization of that condition variable. A condition variable does 82 not need to be explicitly initialized. A condition variable is initialized with 83 all zeros, by default, and its scope is set to within the calling process. For 84 inter-process synchronization, a condition variable must be initialized once, 85 and only once, before use. 86 .sp 87 .LP 88 A condition variable must not be simultaneously initialized by multiple threads 89 or re-initialized while in use by other threads. 90 .sp 91 .LP 92 Condition variables attributes may be set to the default or customized at 93 initialization. POSIX threads even allow the default values to be customized. 94 Establishing these attributes varies depending upon whether POSIX or Solaris 95 threads are used. Similar to the distinctions between POSIX and Solaris thread 96 creation, POSIX condition variables implement the default, intra-process, 97 unless an attribute object is modified for inter-process prior to the 98 initialization of the condition variable. Solaris condition variables also 99 implement as the default, intra-process; however, they set this attribute 100 according to the argument, \fItype\fR, passed to their initialization function. 101 .SS "Condition Wait" 102 .sp 103 .LP 104 The condition wait interface allows a thread to wait for a condition and 105 atomically release the associated mutex that it needs to hold to check the 106 condition. The thread waits for another thread to make the condition true and 107 that thread's resulting call to signal and wakeup the waiting thread. 108 .SS "Condition Signaling" 109 .sp 110 .LP 111 A condition signal allows a thread to unblock the next thread waiting on the 112 condition variable, whereas, a condition broadcast allows a thread to unblock 113 all threads waiting on the condition variable. 114 .SS "Destroy" 115 .sp 116 .LP 117 The condition destroy functions destroy any state, but not the space, 118 associated with the condition variable. 119 .SH ATTRIBUTES 120 .sp 121 .LP 122 See \fBattributes\fR(5) for descriptions of the following attributes: 123 .sp 124 125 .sp 126 .TS 127 box; 128 c | c 129 l | l . 130 ATTRIBUTE TYPE ATTRIBUTE VALUE 131 _ 132 MT-Level MT-Safe 133 .TE 134 135 .SH SEE ALSO 136 .sp 137 .LP 138 \fBfork\fR(2), \fBmmap\fR(2), \fBsetitimer\fR(2), \fBshmop\fR(2), 139 \fBcond_broadcast\fR(3C), \fBcond_destroy\fR(3C), \fBcond_init\fR(3C), 140 \fBcond_signal\fR(3C), \fBcond_timedwait\fR(3C), \fBcond_wait\fR(3C), 141 \fBpthread_cond_broadcast\fR(3C), \fBpthread_cond_destroy\fR(3C), 142 \fBpthread_cond_init\fR(3C), \fBpthread_cond_signal\fR(3C), 143 \fBpthread_cond_timedwait\fR(3C), \fBpthread_cond_wait\fR(3C), 144 \fBpthread_condattr_init\fR(3C), \fBsignal\fR(3C), \fBattributes\fR(5), 145 \fBmutex\fR(5), \fBstandards\fR(5) 146 .SH NOTES 147 .sp 148 .LP 149 If more than one thread is blocked on a condition variable, the order in which 150 threads are unblocked is determined by the scheduling policy. 151 .sp 152 .LP 153 \fBUSYNC_THREAD\fR does not support multiple mapplings to the same logical 154 synch object. If you need to \fBmmap()\fR a synch object to different locations 155 within the same address space, then the synch object should be initialized as a 156 shared object \fBUSYNC_PROCESS\fR for Solaris, and 157 \fBPTHREAD_PROCESS_PRIVATE\fR for POSIX.