c# - 为什么 C# volatile 不保护读写重排序?

标签 c# .net multithreading volatile

根据 this online book ,C# 中的 volatile 关键字不能防止重新排序写入操作后跟读取操作。它给出了这个示例,其中 ab 最终都可以设置为 0,尽管 xyvolatile:

class IfYouThinkYouUnderstandVolatile
{
  volatile int x, y;
 
  void Test1()        // Executed on one thread
  {
    x = 1;            // Volatile write (release-fence)
    int a = y;        // Volatile read (acquire-fence)
    ...
  }
 
  void Test2()        // Executed on another thread
  {
    y = 1;            // Volatile write (release-fence)
    int b = x;        // Volatile read (acquire-fence)
    ...
  }
}

这似乎符合规范在 10.5.3 中所说的:

A read of a volatile field is called a volatile read. A volatile read has “acquire semantics”; that is, it is guaranteed to occur prior to any references to memory that occur after it in the instruction sequence.

A write of a volatile field is called a volatile write. A volatile write has “release semantics”; that is, it is guaranteed to happen after any memory references prior to the write instruction in the instruction sequence.

这是什么原因?是否存在我们不介意重新排序写入-读取操作的用例?

最佳答案

volatile不保证独立volatile变量的读写不重序,它只保证读到的是最新的值(非缓存)。 (保证对单个变量的读写保持顺序)

http://msdn.microsoft.com/en-us/library/x13ttww7%28v=vs.71%29.aspx

The system always reads the current value of a volatile object at the point it is requested, even if the previous instruction asked for a value from the same object. Also, the value of the object is written immediately on assignment.

The volatile modifier is usually used for a field that is accessed by multiple threads without using the lock statement to serialize access. Using the volatile modifier ensures that one thread retrieves the most up-to-date value written by another thread.

每当你有多个依赖操作时,你需要使用一些其他的同步机制。通常使用 lock,这是最简单的,并且只会在滥用或非常极端的情况下造成性能瓶颈。

关于c# - 为什么 C# volatile 不保护读写重排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11747051/

相关文章:

c# - ElasticSearch过滤查询

javascript - 如何启用asp :LinkButton on client side

multithreading - 等待线程而不卡住应用程序

c# - 存储对非静态方法的引用

.net - 无法在 Windows 7 中安装第三方 Windows 服务

java - 如何在 java 2d 射击游戏中将计时器设置为 1 分钟?

Python 线程/子进程;当子进程仍在运行时,线程对象无效

C# 正则表达式错误未终止 [] 集

c# - 查找要在没有命名空间或程序集的情况下按名称实例化的类? (。网)

c# - 什么应该放入 AssemblyTrademarkAttribute 中?