Condition variables?

David Brown dlang at davidb.org
Tue Oct 2 07:39:33 PDT 2007


On Tue, Oct 02, 2007 at 08:27:44AM +0100, Janice Caron wrote:

>I do believe that would deadlock. If thread A was waiting inside a
>synchronized block, then thread B would never be able to enter the add
>function, and hence would never be able to trigger the condition.

Aside from the fact that the example isn't quite right (see my other
posting), condition variables _must_ be taken inside of a synchronize
statement.  The underlying condition variable requires a mutex as well as
the condition variable.  The wait operation atomically unlocks the mutex
and goes to sleep.  Upon re-awakening, it then reacquires the lock without
allowing someone else to sneak in.

This feature is the very magic about condition variables that makes race
free synchronization possible where it isn't with events.

   <http://en.wikipedia.org/wiki/Monitor_(synchronization)>

David



More information about the Digitalmars-d mailing list