Your object d written and read by two streams and is not atomic. This is not safe, as suggested in the C ++ standard for multithreading:
1.10 / 4 . Two evaluations of expressions contradict each other if one of them changes the location of the memory, and the other - or changes the same memory cell.
1.10 / 21 A program execution contains a data race if it contains two conflicting actions in different threads, at least one of which is not atomic, and does not occur before the other. Any such data guest leads to undefined behavior.
Important change:
In your non-nuclear case, you have no guarantees regarding the order between reading and writing. You do not even have a guarantee that the reader will read the value written by the author (this short article explains the risk for non-atomic variables).
However , your reader cycle ends by checking the surrounding atomic variables for which strong guarantees exist. Assuming that version never repeated between different calls to the writer and the reverse order is set in which you acquire your meaning:
- the reading order of
d compared to writing d cannot be unsuccessful if the two atoms are equal. - likewise, the reading value cannot be inconsistent if the two atoms are equal.
This means that in case of an unfavorable race condition on your non-atomic, thanks to the loop, you will finish reading the last value .
source share