Re: JavaMemoryModel: correct synchronization

From: Bill Pugh (pugh@cs.umd.edu)
Date: Fri Feb 20 2004 - 18:26:59 EST


On Feb 20, 2004, at 5:16 PM, Adam Welc wrote:
>
>
> Is the following program correctly synchronized?
>
>
> public static boolean w = false;
> public static boolean v = false;
>
> T1 T2
> w=true; while(true) {
> synchronized(M) { synchronized(M) {
> v =true; if (v) break;
> } }
> }
> boolean tmp=w;

Unless I'm missing something, this program is correctly synchronized.

All accesses to v are guarded by the lock on M.
If T1 and T2 both access w, you know that T1 must have released a lock
on M that
was then acquired by T2 before T2 reads w. So accesses to w are
correctly synchronized.

What makes you think this program may not be correctly synchronized?

Bill

-------------------------------
JavaMemoryModel mailing list - http://www.cs.umd.edu/~pugh/java/memoryModel



This archive was generated by hypermail 2b29 : Thu Oct 13 2005 - 07:00:57 EDT