Get a Reference to an Object's Monitor

Sean Kelly sean at invisibleduck.org
Tue Dec 20 21:16:40 PST 2011


Yup. 

Sent from my iPhone

On Dec 20, 2011, at 4:22 PM, Andrew Wiley <wiley.andrew.j at gmail.com> wrote:

> On Tue, Dec 20, 2011 at 3:40 PM, bearophile <bearophileHUGS at lycos.com> wrote:
>> Andrew Wiley:
>> 
>>> I suspect the reason they advise not using it in user code is that the
>>> monitor is lazily initialized (if I remember correctly), so I'd have
>>> to initialize it myself.
>> 
>> I see.
> 
> It looks like Sean is ahead of me:
> From core.sync.mutex:
> 
> this(Object o);
> Initializes a mutex object and sets it as the monitor for o.
> 
> So my example would look like:
> ---
> synchronized class Example {
> private:
>   Condition _condition;
> public
>   this() {
>       auto lock = new Mutex(this);
>       _condition = new Condition(lock);
>   }
>   void methodA() {
>       // do some stuff
>       while(someTest)
>           _condition.wait();
>   }
>   void methodB() {
>       //do some stuff
>       _condition.notifyAll();
>   }
> }
> ---


More information about the Digitalmars-d mailing list