Get a Reference to an Object's Monitor

Andrew Wiley wiley.andrew.j at gmail.com
Tue Dec 20 16:22:44 PST 2011


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