Reputation: 433
Can I say that if a variable can be modified by other thread, I can never safely read it without a memory barrier?
Upvotes: 2
Views: 248
Reputation: 328608
Yes pretty much. If you write (w) to a variable in thread T1 and read (r) that same variable from thread T2, you need to have a happens-before relationship between (w) and (r) to get the guarantee that the result of (w) will be visible to (r). The Java Memory Model defines (JLS 17.4.5) the situations where there is a happens before relationship:
Upvotes: 2