[concurrency-interest] Volatile happens before question

Boehm, Hans hans.boehm at hp.com
Tue Jan 17 18:13:15 EST 2012


Another way to look at this is that compiler reordering that is only observable by programs with data races is generally allowed.  The program below clearly has an inherent data race on b.  If you make b volatile, the data race, and the problem, go away.

Hans

> -----Original Message-----
> From: concurrency-interest-bounces at cs.oswego.edu [mailto:concurrency-
> interest-bounces at cs.oswego.edu] On Behalf Of David Holmes
> Sent: Tuesday, January 17, 2012 2:50 PM
> To: Raph Frank
> Cc: concurrency-interest at cs.oswego.edu
> Subject: Re: [concurrency-interest] Volatile happens before question
> 
> Raph Frank writes:
> > I was wondering if there was a happens before guarantee for reads of
> > volatiles relative to later writes.  I think maybe not, but it isn't
> > clear.
> 
> The specification defines all the happens-before orderings that exist -
> see
> JLS 17.4.5 for the definitions of happens-before, and 17.4.4 for the
> defined
> synchronization actions. In terms of your question a volatile write
> happens-before subsequent reads of the same variable. There is no
> happens-before ordering between a volatile read and a subsequent
> volatile
> write.
> 
> > For example, assuming there were 2 threads with the following code:
> >
> > ** Thread 1 **
> >
> > int b = 0;
> > volatile boolean a = false;
> > ...
> > ...
> > a = true;
> > b = 1;
> >
> > ** Thread 2 **
> >
> > int bStore = b;
> > if (!a) {
> >   System.out.println("The value of bStore is " + bStore);
> > }
> >
> > Are these reasonable statements:
> >
> > "bStore = b" happens-before "if(!a)"
> 
> Yes - these are actions in the same thread and so happens-before order
> follows program order
> 
> > "if(!a)" happens-before "a = true;" [Assuming that (!a) evaluates to
> true]
> 
> No. You need to be careful about "happens-before" vs. "occurred
> before". The
> former is a clearly defined property in the memory-model. The latter is
> the
> actual occurrence at execution time. If !a evaluates to true then it is
> certainly the case that the read of 'a' occurred prior to the write
> a=true.
> But there is no happens-before edge between the two actions.
> 
> > "a = true;" happens-before "b=1;"
> 
> Yes - these are actions in the same thread and so happens-before order
> follow program order
> 
> > Thus, the program will either print "The value of bStore is 0" or not
> > print anything.
> 
> No. You lost the transitive relationship of happens-before with your
> second
> statement. Your code can be reordered and executed as follows:
> 
>     Thread 1                    Thread 2
>                                 if (!a) // sees false
>     a = true
>     b = 1;
>                                 bStore =b;
>                                 print   // prints 1
> 
> Or as Vitaly states, the assignments in Thread 1 could appear reordered
> when
> seen from Thread 2
> 
> Cheers,
> David Holmes
> 
> 
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest



More information about the Concurrency-interest mailing list