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

Jonathan M Davis jmdavisProg at gmx.com
Mon May 28 16:41:51 PDT 2012


On Tuesday, May 29, 2012 01:35:23 Alex Rønne Petersen wrote:
> I don't think arguing about them makes sense at this point. Way too much
> code would break if we changed the semantics. I'd consider it a mistake
> and a lesson learned, rather.
> 
> But I take it you agree that synchronized (this) and similar
> "uncontrollable" synchronization blocks should be avoided?

I'm not an expert on threading stuf, but it would be my opinion that if you're 
not intending to protect the entire class with locks that it makes no sense to 
lock on the class itself. You're locking for something specific, in which case, 
your sychronized block should be locking on something else specific to what 
you're trying to protect. Certainly, that's how I'd approach it with mutexes. 
You don't have a mutex for an entire class unless it's actually used for all 
of the class' functions. Rather, you use mutexes specific to what you're trying 
to protect.

- Jonathan M Davis


More information about the Digitalmars-d mailing list