Hello,
The reason I had doubts is that the JMM only talks about the program
behavior with respect do shared data accesses. Th JMM does not even
mention other language constructs like conditionals or loops. It seemed to
me that it should be possible to determine if a program is correctly
synchronized without analyzing effects that these other language
constructs have on the execution of a program. This however is not the
case in the example being discussed.
What I was trying to achieve was to try to come up with a rule that a
programmer could follow to write race-free concurrent programs. It seems
to me that a sufficient condition is to protect every access to a shared
variable by the same monitor (unless all the accesses are reads). Is
that correct?
Then I was trying to determine if this is also a necessary condition, and
that does not seem to be true, as the example indicates.
Best regards
Adam Welc
On Fri, 20 Feb 2004, 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
>
-- Adam Welc Computer Science Building, room 274 West Lafayette, IN 47906 Telephone number (work): (765) 4947836http://www.cs.purdue.edu/homes/welc
------------------------------- 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