In .NET, the Thread class has the functions VolatileRead and VolatileWrite (and similar Volatile.Read / Volatile.Write ):
[MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations public static int VolatileRead(ref int address) { int ret = address; MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way. return ret; } [MethodImplAttribute(MethodImplOptions.NoInlining)] // disable optimizations public static void VolatileWrite(ref int address, int value) { MemoryBarrier(); // Call MemoryBarrier to ensure the proper semantic in a portable way. address = value; } I do not quite understand why memory barriers stand in these places. For example, in VolatileRead , why not need a barrier to reading the value? In addition, these methods are not inline, can the processor rearrange instructions through call / ret?