79540878

Date: 2025-03-28 08:29:38
Score: 1
Natty:
Report link

Is it true, that reader, when reading from a field states gets a valid value of states, that existed at one point of the time (linearizability is present)? Can reader thread read some nonsense when reading value of states due to compiler reordering?

In your example each read of states has no happens-before ordering with writes to states.
According to the JMM that means every read is allowed to return any of the writes to states that happen anytime during the program execution.
This includes writes to states that happen later (in wall-clock time) in the same execution.
This also includes null - the default initialization value for states.

Here's the quote from the JLS:

Informally, a read r is allowed to see the result of a write w if there is no happens-before ordering to prevent that read.

To sum up:

Reasons:
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Unregistered user (0.5):
  • Starts with a question (0.5): Is it
  • Low reputation (1):
Posted by: Андрей