就我对这些技巧的理解而言,能够在 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.
但是,MemoryBarrier
、VolatileRead
和 VolatileWrite
在 Thread
类上均不可用。
我的问题是:
- 这是一个确定的选择吗?
- 实现无锁功能的最佳替代方案是什么(假设我当前的代码库使用
MemoryBarrier
)?
最佳答案
实际上 Thread.MemoryBarrier()
方法已移至 Interlocked.MemoryBarrier()
。
这种 Interlocked
方法在 .Net 4.5(和 4.6)和 DNXCORE50 上可用:这绝对是从现在开始使用的方法。
请注意,VolatileRead\Write
不可用。
关于c# - DNX Core 5.0 中的 Thread.MemoryBarrier() 和其他内存栅栏功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33450609/