Re: JavaMemoryModel: correct synchronization

From: Tony Hosking (hosking@cs.purdue.edu)
Date: Fri Feb 20 2004 - 21:38:23 EST


I guess the question is, does the JMM guarantee that T2 sees w == true.
  Since w is not protected by a monitor is it not reasonable to read w
in T2 before T1 releases M?

On 20/02/2004, at 6:26 PM, Bill Pugh wrote:

>
> 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
>
>
Antony Hosking | Associate Professor
Dept of Computer Sciences | Office: (765) 494-6001
_--_|\
Purdue University | Mobile: (765) 427-5484 /
  \
250 N. University Street | Email:hosking@cs.purdue.edu
\_.--._/ )
West Lafayette, IN 47907-2066 | http://www.cs.purdue.edu/~hosking
v /

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



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