I have a question about the following code sample (m_value isn\'t volatile, and every thread runs on a separate processor)
void Foo() // executed by thr
If m_value
is not marked as volatile
, then there is no reason to think that the value read in Bar
is fenced. Compiler optimizations, caching, or other factors could reorder the reads and writes. Interlocked exchange is only helpful when it is used in an ecosystem of properly fenced memory references. This is the whole point of marking a field volatile
. The .Net memory model is not as straight forward as some might expect.