[concurrency-interest] Volatile happens before question

Raph Frank raphfrk at gmail.com
Tue Jan 17 12:12:22 EST 2012


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.



More information about the Concurrency-interest mailing list