views:

143

answers:

1

D2.0 classes have a __monitor class property that "gives access to the class object's monitor" (documentation). I searched around a bit and did not find any information except for this bit of detail. So: what is a monitor? Why is one monitor used for all synchronized member functions? Is it a synchronization primitive used for synchronizing member functions similar to Java? And why is the __monitor property in the language def if you are not supposed to use it / what are the use-cases?

+5  A: 

The monitor is a lazily initialized object that all synchronized methods synchronize on, just like in Java. Unlike Java, D is a systems programming language and exposes lower level details of how things work just in case you need to hack them, even if doing so is usually a bad idea. This allows you to customize behavior. For example, it is possible to customize the monitor object of a class, or to use a core.sync.mutex that shares a monitor with the class that owns it.

dsimcha
stephan
@stephan: I'm not sure, I've never actually had to mess with this before, so i don't know the details of it.
dsimcha
I have played a little bit around with it. *Seems* to work like this.
stephan
You shouldn't have to ever implement the monitor or set it yourself unless you're doing something *very* strange. I've used the various synchronisation primitives in D a few times and never had to do anything of the sort.
DK