D Mutex reference.

Frits van Bommel fvbommel at REMwOVExCAPSs.nl
Fri Jan 19 08:06:38 PST 2007


Saaa wrote:
> Doesn't D have mutexes?
> Or better: What is the advantage of synchronized over the use of mutexes?

Every object has an associated mutex. (technically it's created on first 
use of that object in a "synchronized(obj) {}" statement)

> Could you explain this line:
> static this() {
> lock=new Object;
> }

This initializes the lock variable at the start of the program. IIRC you 
can't new an object in the initializer of a static function, so this is 
how you do it.

> Does this mean I need to enable the GC?(I mean, not disable it :)

Disabling the GC only disables the actual collection, not the allocation 
of objects. Since it's in a global variable (in the example) it will 
never need to be collected.
If you only need a mutex temporarily you can create an Object, make a 
reference to it available to the various threads that need it, and 
delete it when you're done with it. No GC required.


More information about the Digitalmars-d-learn mailing list