[concurrency-interest] Volatile happens before question

Vitaly Davidovich vitalyd at gmail.com
Tue Jan 17 12:56:52 EST 2012


b=1 is allowed to move before a=true so you can read a=false and see b as 1.

Sent from my phone
On Jan 17, 2012 12:14 PM, "Raph Frank" <raphfrk at gmail.com> wrote:

> 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.
>
> 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)"
> "if(!a)" happens-before "a = true;" [Assuming that (!a) evaluates to true]
> "a = true;" happens-before "b=1;"
>
> Thus, the program will either print "The value of bStore is 0" or not
> print anything.
>
> _______________________________________________
> Concurrency-interest mailing list
> Concurrency-interest at cs.oswego.edu
> http://cs.oswego.edu/mailman/listinfo/concurrency-interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://cs.oswego.edu/pipermail/concurrency-interest/attachments/20120117/9ffeb437/attachment.html>


More information about the Concurrency-interest mailing list