[dmd-concurrency] synchronized, shared, and regular methods inside the same class

Andrei Alexandrescu andrei at erdani.com
Mon Jan 4 20:57:29 PST 2010


Sean's code has an error: mutateA takes an A (as opposed to a shared A) 
so no synchronization is assumed or necessary.

Again: non-shared objects _are_ thread-local objects.


Andrei

Michel Fortin wrote:
> Le 2010-01-04 à 22:49, Sean Kelly a écrit :
> 
>>    class A {
>>        void fnA() synchronized { fnB(); } // 1
>>        private void fnB() synchronized {}
>>    }
>>
>>    void mutateA( A val ) {
>>        val.fnB(); // 2
>>    }
>>
>> At 1, the compiler can easily tell that the lock is already held so it doesn't acquire the lock again.  At 2 the lock is (probably) not held, so it's acquired again.
> 
> To avoid that "lock is probably not held" situation, we could allow the standalone function to be declared like this:
> 
> 	void mutateA(synchronized A val) {
> 	    val.fnB();
> 	}
> 
> Now 'val' can be locked at the call site, if necessary.
> 


More information about the dmd-concurrency mailing list