gpt4 book ai didi

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

转载 作者:IT王子 更新时间:2023-10-29 04:50:14 24 4
gpt4 key购买 nike

根据 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com