Can I say that if a variable can be modified by other thread, I can never safely read it without a memory barrier?
Share
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
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: