Search code examples
c#multithreadinglock-freememory-model

Using memory barriers


In the following code sample, does the memory barrier in FuncA is required to ensure that the most up-to-date value is read?

class Foo
{
   DateTime m_bar;

   void FuncA() // invoked by thread X
   {
      Thread.MemoryBarrier(); // is required?
      Console.WriteLine(m_bar);
   }

   void FuncB() // invoked by thread Y
   {
       m_bar = DateTime.Now;
   }       
}

EDIT: If not, how can I ensure that FuncA will read the most recent value? (I want to make sure that the recent value is actually store in the processor's cache) [wihout using locks]


Solution

  • This actually doesn't matter since on 32bit architectures one can get a torn read in such situation