synchronized / cond.wait

Sergey Gromov snake.scaly at
Tue Mar 31 06:54:21 PDT 2009

Tue, 31 Mar 2009 15:37:33 +0200, Stefan Rohe wrote:

> could someone explain us this "synchronize" behaviour? We do not understand
> it.
> Two Threads.
> First Thread should wait for a condition. This we do in a
> synchronized(mutex) block.
> The second Thread broadcasts then a notify. This also in a synchronized
> block, synchronizing on the same mutex. This should be impossible!? Does the
> condition.wait modify the Mutex object so that it after this has another
> address?

The whole point of a condition variable is to allow other threads to
acquire the same mutex while you're waiting.

What happens is the mutex associated with the conditional variable is
unlocked the same instant you start waiting on that variable.  When the
other thread notifies you, the cond var attempts to lock the mutex again
and therefore waits until the other thread either leaves the critical
section or starts waiting itself.

More information about the Digitalmars-d-learn mailing list