From MSDN, Volatile.Read()
:
Reads the value of a field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears after this method in the code, the processor cannot move it before this method.
and Volatile.Write()
:
Writes a value to a field. On systems that require it, inserts a memory barrier that prevents the processor from reordering memory operations as follows: If a read or write appears before this method in the code, the processor cannot move it after this method.
I think I can understand the using scenarios of Volatile.Read()
and Volatile.Write()
, and have seen many examples explaining why these two methods help ensuring the correctness of program.
But I still wonder, what is the logic behind these rules?
Take Volatile.Read()
as example, why it requires operations after it cannot be moved before it, but does not require anything from operations before it?
And also why it's opposite to Volatile.Write()
?
Thank you!