<html><body><div style="color:#000; background-color:#fff; font-family:arial, helvetica, sans-serif;font-size:12pt"><div><span>A condition variable is logically attached to an arbitrary condition as defined in your code (such as, there is an element in a queue).  The proper sequence for using a condition is:</span></div><div><br><span></span></div><div><span>lock(mutex);</span></div><div><span>while(logicalConditionIsFalse) cond.wait();</span></div><div><span>// processCondition, e.g. remove element from queue</span></div><div><span>unlock(mutex);<br></span></div><div><br><span></span></div><div><span>It doesn't work like an event in Windows, and events in windows do *not* protect external conditions, the event *is* the condition.</span></div><div><span><br></span></div><div><span>For mutex/conditions, only threads waiting *at the time* you signal a condition will be woken up.  So you have to have a mutex to protect the state of the condition,
 otherwise, you might miss the condition signal.  You also avoid needless waiting/locking.<br></span></div><div><br><span></span></div><div><span>You might build mutex/conditions into a more high-level object (such as a queue), which would hide the details, but the primitives are correct, and time-tested.<br></span></div><div><br></div><div>Now, I'm all for a use case of storing the mutex in the condition and having the condition "own" the mutex, but that should not be a limitation, you should be able to attach multiple conditions to the same mutex, which means none of them own it.</div><div><br><span></span></div><div><span>-Steve<br></span></div><div><br><blockquote style="border-left: 2px solid rgb(16, 16, 255); margin-left: 5px; margin-top: 5px; padding-left: 5px;">  <div style="font-family: arial, helvetica, sans-serif; font-size: 12pt;"> <div style="font-family: times new roman, new york, times, serif; font-size: 12pt;"> <div dir="ltr"> <font
 size="2" face="Arial"> <hr size="1">  <b><span style="font-weight:bold;">From:</span></b> Alex Rønne Petersen <xtzgzorex@gmail.com><br> <b><span style="font-weight: bold;">To:</span></b> D's runtime library developers list <d-runtime@puremagic.com> <br> <b><span style="font-weight: bold;">Sent:</span></b> Wednesday, April 25, 2012 8:18 PM<br> <b><span style="font-weight: bold;">Subject:</span></b> Re: [D-runtime] What's with core.sync.condition usage?<br> </font> </div> <br>Surely it could lock whatever it needs to lock internally?<br><br>See: <a href="http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx" target="_blank">http://msdn.microsoft.com/en-us/library/system.threading.autoresetevent.aspx</a><br><br>That class is entirely thread safe.<br><br>Regards,<br>Alex<br><br>On Thu, Apr 26, 2012 at 1:17 AM, Jonathan M Davis <<a ymailto="mailto:jmdavisProg@gmx.com"
 href="mailto:jmdavisProg@gmx.com">jmdavisProg@gmx.com</a>> wrote:<br>> On Thursday, April 26, 2012 00:41:40 Alex Rønne Petersen wrote:<br>>> Hi,<br>>><br>>> What's with the weird, convoluted way one is supposed to use<br>>> core.sync.condition.Condition? Having to lock on a mutex while using a<br>>> Condition object is extremely weird compared to APIs in other<br>>> programming languages. Any particular reason behind this design?<br>><br>> Really? In C++, from what I've seen, you always end up locking on a mutex to<br>> protect a condition variable. How could it work _without_ locking? The<br>> condition variable wouldn't be protected against multiple threads using it.<br>><br>> - Jonathan M Davis<br>> _______________________________________________<br>> D-runtime mailing list<br>> <a ymailto="mailto:D-runtime@puremagic.com"
 href="mailto:D-runtime@puremagic.com">D-runtime@puremagic.com</a><br>> <a href="http://lists.puremagic.com/mailman/listinfo/d-runtime" target="_blank">http://lists.puremagic.com/mailman/listinfo/d-runtime</a><br>_______________________________________________<br>D-runtime mailing list<br><a ymailto="mailto:D-runtime@puremagic.com" href="mailto:D-runtime@puremagic.com">D-runtime@puremagic.com</a><br><a href="http://lists.puremagic.com/mailman/listinfo/d-runtime" target="_blank">http://lists.puremagic.com/mailman/listinfo/d-runtime</a><br><br><br> </div> </div> </blockquote></div>   </div></body></html>