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

Michel Fortin michel.fortin at michelf.com
Mon Jan 4 20:54:36 PST 2010


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.

-- 
Michel Fortin
michel.fortin at michelf.com
http://michelf.com/





More information about the dmd-concurrency mailing list