Consider two threads:
A==B==0 (initially)
Thread 1 | Thread 2 |
---|---|
B=42; | if (A==1) |
A=1; | ...print(B) |
To my knowledge if (at least) A
is volatile
we will only be able to read B==42
at the print
. Though if only mark B
as volatile
we can read B==42
but also B==0
.
I want to look at the case where only B
is volatile
more closely and understand why we can read B==0
based on what these docs say. To do so I started by adding all program order edges and synchronizes with as described in the docs:
The two edges from B=42
to A=1
are simple program order (PO) edges the rest are synchronizes with (SW) edges. According to the docs we have a SW edge when "The write of the default value [...] to each variable synchronizes-with the first action in every thread." (those are the first 4 edges in the picture) and "A write to a volatile variable v [...] synchronizes-with all subsequent reads of v" (the edges from B=42
to print(B)
).
Now we can take a look at what happens before edges exists (HB), according to the docs each of these edges is also an HB ordering. Additionally for all hb(x,y) and hb(y,z) we have hb(x,z) (these edges are missing but we will still use those).
Finally, we get from the docs what we can read at print(B)
from: "We say that a read r of a variable v is allowed to observe a write w to v if, in the happens-before partial order [...]:
- r is not ordered before w (i.e., it is not the case that hb(r, w)), and
- there is no [...] no write w' to v such that hb(w, w') and hb(w', r) "
Let's see if we can observe a write w (B=0
) at a read r (print(B)
). We indeed have not hb(r, w). However, we do have a write w' (B=42) intervening with hb(wow') and hb(w',r).
This makes me wonder can we observe B==0
at the print and if yes where is my reasoning or understanding of the docs wrong? I would like a answer that is clearly referring to the docs.
(I have looked at this post however I hope for an explanation referencing the JMM docs more closely, my question also arises from this particular code)