synchronized (this[.classinfo]) in druntime and phobos

Martin Nowak dawg at dawgfoto.de
Wed May 30 10:20:02 PDT 2012


On Wed, 30 May 2012 01:10:41 +0200, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 5/29/12 4:06 PM, Alex Rønne Petersen wrote:
>>  Synchronized blocks are good because they
>> operate on an implicit, hidden, global mutex. You can't screw up with  
>> that.
>
> I think there's quite some disconnect here. If there's any anti-pattern  
> in this discussion, it's operating on an implicit, hidden, global mutex.  
> Walter agreed to eliminate that from D, but never got around to it.
>

They're not really global, it's one per synchronized block.
https://github.com/D-Programming-Language/druntime/blob/master/src/rt/critical_.d
They're actually pretty limited/safe because you can't access/compose the  
underlying lock.

import std.stdio, core.thread;

void foo()
{
     synchronized
     {
         auto t = new Thread(&bar);
         t.start();
         t.join();
         writeln("foo");
     }
}

void bar()
{
     synchronized writeln("bar");
}

void main()
{
     foo();
}


More information about the Digitalmars-d mailing list