Get a Reference to an Object's Monitor

Andrew Wiley wiley.andrew.j at gmail.com
Tue Dec 20 15:08:58 PST 2011


On Tue, Dec 20, 2011 at 2:29 PM, bearophile <bearophileHUGS at lycos.com> wrote:
> Andrew Wiley:
>
>> Is there any way to get a reference to an object's monitor?
>
> Is this useful? From the D2 docs:
>
>> The properties .__vptr and .__monitor give access to the class object's vtbl[] and
>> monitor, respectively, but should not be used in user code.

I've seen that, but using the built-in monitor explicitly in a few
places means it's much harder to make mistakes like locking the wrong
lock or forgetting to lock altogether. In my two code examples, the
first one is much easier to screw up as the class gets larger and
larger because marking methods as "shared" eliminates any guarantees
the compiler can provide.
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.


More information about the Digitalmars-d mailing list