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

Regan Heath regan at netmail.co.nz
Wed May 30 09:43:30 PDT 2012


On Wed, 30 May 2012 17:00:43 +0100, Andrei Alexandrescu  
<SeeWebsiteForEmail at erdani.org> wrote:

> On 5/30/12 5:32 AM, Regan Heath wrote:
>> On Wed, 30 May 2012 10:21:00 +0100, deadalnix <deadalnix at gmail.com>  
>> wrote:
>>> You don't want to synchronize on ANY object. You want to synchronize
>>> on explicit mutexes.
>>
>> +1 .. this is the key point/issue.
>
> TDPL's design only allows for entire synchronized classes (not separate  
> synchronized and unsynchronized methods), which pair mutexes with the  
> data they protect. This is more restrictive than exposing mutexes, but  
> in a good way. We use such a library artifact in C++ at Facebook all the  
> time, to great success.

Can you call pass them to a synchronized statement?  i.e.

TDPLStyleSynchClass a = new TDPLStyleSynchClass();
synchronized(a) {
}

.. because, if you can, then you're exposing the mutex.  If you can't, how  
do you do the above with these classes?  By explicitly calling a lock() or  
tryLock() method?  (do they implement a known interface?)

> People shouldn't create designs that have synchronized classes referring  
> to one another naively. Designing with mutexes (explicit or implicit)  
> will always create the possibility of deadlock, so examples how that  
> could happen are easy to come across.

True.  But in my Example 1 the classes could be "entire" synchronized  
classes, and they do not refer to each other naively.  Instead, two  
threads have shared access to them and in each case they explicitly  
acquire one mutex (via a synchronized() statement), but implicitly acquire  
another (by calling a method).  It's the implicit acquisition which makes  
the bug hard to see and easy to do accidentally.

> TDPL improves on deadlocks by introducing synchronized statements with  
> more than one argument, see 13.15.

Is there anywhere I can see this online? (for free :p)

> The implicit mutexes used by synchronized classes are recursive.

:) why would you want anything else.

R

-- 
Using Opera's revolutionary email client: http://www.opera.com/mail/


More information about the Digitalmars-d mailing list