Thread.MemoryBarrier() 和 DNX Core 5.0 中的其他内存栅栏功能

Thread.MemoryBarrier() and other memory fences capabilities in DNX Core 5.0

就我对这些技巧的理解而言,能够在 DNX 上完成完整的全栅栏内存屏障比在标准 .Net 框架中更重要: - DNX 可能 运行 在内存模型比 x86/x64 弱的 IA64 上。 - Microsoft CLR 使用比 ECMA 规范定义的更强大的内存模型。

引用Why do I need a memory barrier?

的回答

You are going to have a very hard time reproducing this bug. In fact, I would go as far as saying you will never be able to reproduce it using the .NET Framework. The reason is because Microsoft's implementation uses a strong memory model for writes. That means writes are treated as if they were volatile. A volatile write has lock-release semantics which means that all prior writes must be committed before the current write.

However, the ECMA specification has a weaker memory model. So it is theoretically possible that Mono or even a future version of the .NET Framework might start exhibiting the buggy behavior.

但是,MemoryBarrierVolatileReadVolatileWriteThread class 上均不可用。

我的问题是:

实际上 Thread.MemoryBarrier() 方法已移至 Interlocked.MemoryBarrier()

Interlocked 方法在 .Net 4.5(和 4.6)和 DNXCORE50 上可用:这绝对是从现在开始使用的方法。

请注意 VolatileRead\Write 不可用。