Does Java volatile read flush writes, and does volatile write update reads

Viewed 1903

I understand read-acquire(does not reorder with subsequent read/write operations after it), and write-release(does not reorder with read/write operations preceding it). My q is:-

  1. In case of read-acquire, do the writes preceding it get flushed?
  2. In case of write-release, do the previous reads get updated?

Also, is read-acquire same as volatile read, and write release same as volatile write in Java?

Why this is important is that, let's take case of write-release..

y = x; // a read.. let's say x is 1 at this point
System.out.println(y);// 1 printed
//or you can also consider System.out.println(x);
write_release_barrier();
//somewhere here, some thread sets x = 2
ready = true;// this is volatile
System.out.println(y);// or maybe, println(x).. what will be printed?

At this point, is x 2 or 1? Here, consider ready to be volatile. I understand that all stores before volatile will first be made visible.. and then only the volatile will be made visible. Thanks.

Ref:- http://preshing.com/20120913/acquire-and-release-semantics/

4 Answers
Related