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

标签 c# .net multithreading dnx memory-barriers

就我对这些技巧的理解而言,能够在 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 类上均不可用。

我的问题是:

  • 这是一个确定的选择吗?
  • 实现无锁功能的最佳替代方案是什么(假设我当前的代码库使用 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/

相关文章:

c# - 订阅 ObservableCollection 项目属性已更改 - WPF

c# - .NET 中的单例在所有 session 之间共享吗?

c# - 函数的监听器无法启动。为什么?

c# - 不确定为什么 Regex.Replace() 在使用包含正则表达式模式(涉及捕获组)的字典时不起作用

c# - 如何从表格中选择内容并输出到标签?

c# - 默认程序集绑定(bind)行为

c# - 有什么办法可以将这两种方法合并为一种方法,或者重载方法吗?

c++ - 使用ACE时有没有办法使用线程局部变量?

java - 当需要多次设置和取消时,如何在 Java 中使用计时器?

c++ - 不能使用线程构造函数