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

Alex Rønne Petersen alex at lycus.org
Mon May 28 15:36:13 PDT 2012


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?

-- 
Alex Rønne Petersen
alex at lycus.org
http://lycus.org


More information about the Digitalmars-d mailing list