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

Sean Kelly sean at invisibleduck.org
Thu May 31 08:39:06 PDT 2012


On May 31, 2012, at 2:48 AM, Andrei Alexandrescu <SeeWebsiteForEmail at erdani.org> wrote:

> On 5/31/12 2:36 AM, Regan Heath wrote:
>> On Wed, 30 May 2012 19:29:39 +0100, Andrei Alexandrescu
>> <SeeWebsiteForEmail at erdani.org> wrote:
>>> You can have deadlocks but with synchronized you can't leak locks or
>>> doubly-unlock them. With free mutexes you have all of the above.
>> 
>> I'm not suggesting using free mutexes. I'm suggesting keeping the mutex
>> private inside the object.
> 
> Ergo, you are suggesting using free mutexes. Your second sentence destroys the first.

To be fair:

auto m = new Mutex;
synchronized (m) {...}

Free mutexes but still safe. Scope guards obviously work too. That said, I think the point of contention here is that because synchronized can take an arbitrary object, it's possible to lock on a class for stuff completely unrelated to what the class does internally. It's certainly bad form though, and I don't know a good way to prevent it without crippling synchronized. 


More information about the Digitalmars-d mailing list