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

deadalnix deadalnix at gmail.com
Tue May 29 01:32:45 PDT 2012


Le 29/05/2012 00:36, Alex Rønne Petersen a écrit :
> Hi,
>
> I've seen several occurrences of synchronized (this) and synchronized
> (this.classinfo) in both druntime and phobos by now. I propose that we
> officially ban these patterns with extreme prejudice.
>
> 1) Locking on the object instance is a HORRIBLE IDEA. Anyone who happens
> to use the object for locking will most likely end up with a deadlock on
> their hands.
> 2) Locking on something as fundamental as type info means that any
> arbitrary part of the application could cause a deadlock by doing the same.
>
> The solution to (1) is to simply use a Mutex internally (or an Object
> with synchronized statements), and for (2), to simply use private global
> objects.
>
> (Now, regarding (1), you might argue that anyone who locks on an
> arbitrary object is doing it wrong, but you can't really blame them -
> it's frankly D's fault for allowing monitors on arbitrary objects, which
> is a horrible mess.)
>
> Anyone against this?
>

I already did some comment about this.

Making any object synchronization is a very bad design decision. It is 
deadlock prone, it is liquid lock prone, it cause an overhead on any 
object, and even worse, it is useless most of the time as D promote 
thread locality (which is very good).

OOP and concurrency are 2 orthogonal topics, and should be handled as such.


More information about the Digitalmars-d mailing list