uri*_*ium 3 c# volatile cpu-architecture lock-free compare-and-swap
以下示例来自MSDN。
public class ThreadSafe
{
// Field totalValue contains a running total that can be updated
// by multiple threads. It must be protected from unsynchronized
// access.
private float totalValue = 0.0F;
// The Total property returns the running total.
public float Total { get { return totalValue; }}
// AddToTotal safely adds a value to the running total.
public float AddToTotal(float addend)
{
float initialValue, computedValue;
do
{
// Save the current running total in a local variable.
initialValue = totalValue;
// Add the new value to the running total.
computedValue = initialValue + addend;
// CompareExchange compares totalValue to initialValue. If
// they are not equal, then another thread has updated the
// running total since this loop started. CompareExchange
// does not update totalValue. CompareExchange returns the
// contents of totalValue, which do not equal initialValue,
// so the loop executes again.
}
while (initialValue != Interlocked.CompareExchange(ref totalValue,
computedValue, initialValue));
// If no other thread updated the running total, then
// totalValue and initialValue are equal when CompareExchange
// compares them, and computedValue is stored in totalValue.
// CompareExchange returns the value that was in totalValue
// before the update, which is equal to initialValue, so the
// loop ends.
// The function returns computedValue, not totalValue, because
// totalValue could be changed by another thread between
// the time the loop ends and the function returns.
return computedValue;
}
}
Run Code Online (Sandbox Code Playgroud)
不应该将 totalValue 声明为 volatile 以获得可能的最新鲜值吗?我想如果你从 CPU 缓存中得到一个脏值,那么对 Interlocked.CompareExchange 的调用应该负责获取最新的值并导致循环再次尝试。volatile 关键字可能会节省一个不必要的循环吗?
我想并不是 100% 都需要 volatile 关键字,因为该方法具有采用不支持 volatile 关键字的 long 等数据类型的重载。
不,volatile根本没有帮助,当然不是因为这个原因。它只会给出首次读取的“获取”语义,而不是有效地放松,但无论哪种方式都会编译为运行加载指令的类似 asm。
如果你从 CPU 缓存中得到一个脏值
CPU 缓存是一致的,因此您从 CPU 缓存中读取的任何内容都是该行的当前全局同意值。“脏”只是意味着它与 DRAM 内容不匹配,并且如果/当被驱逐时必须回写。加载值也可以从存储缓冲区转发,对于该线程最近存储的尚未全局可见的值,但这很好,互锁方法是完全屏障,导致等待存储缓冲区耗尽。
如果你的意思是陈旧的,那么不,那是不可能的,像 MESI 这样的缓存一致性协议可以防止这种情况发生。这就是为什么如果缓存线已经由这个核心拥有(MESI 修改或独占状态),像 CAS 这样的互锁事物不会非常慢。请参阅Myths Programmers Being about CPU Caches,其中讨论了一些 Java volatiles,我认为它们类似于 C# volatile。
这个 C++11 答案还解释了一些关于缓存一致性和 asm 的内容。(请注意,C++11volatile与 C# 显着不同,并不意味着任何线程安全或排序,但仍然意味着 asm 必须执行加载或存储,而不是优化为寄存器。)
在非 x86 上,在您甚至尝试CAS之前,在初始读取之后运行额外的屏障指令(以赋予这些获取语义)只会减慢速度。(在包括 x86-64 在内的 x86 上,易失性读取编译为与普通读取相同的 asm,但它会阻止编译时重新排序)。
如果当前线程只是通过非互锁=分配写了一些东西,则无法将易失性读取优化为仅使用寄存器中的值。这也没有帮助;如果我们只是存储一些东西并在寄存器中记住我们存储的内容,那么从存储缓冲区进行存储转发的加载在道德上等同于仅使用寄存器值。
大多数无锁原子的良好用例是在争用较低的情况下,因此通常事情可以成功,而硬件不必等待很长时间才能访问/拥有缓存线。因此,您希望尽快处理无争议的案例。volatile即使在竞争激烈的情况下有任何好处,也要避免,我认为无论如何都没有。
如果您曾经做过任何简单的存储(带有=,而不是互锁 RMW 的分配),volatile也会对这些产生影响。如果 C#volatile提供类似 C++ 的语义,这可能意味着在此线程中的后续内存操作可以运行之前等待存储缓冲区耗尽memory_order_seq_cst。在那在这种情况下,如果您不需要订购 wrt,您会大大减慢涉及商店的代码。其他加载/存储。如果您在此 CAS 代码之前进行了这样的存储,是的,您将等到该存储(以及所有以前的存储)在全局可见时尝试重新加载它。这意味着 CPU 正在等待的重新加载 + CAS 很可能不必旋转,因为 CPU 将拥有该行的所有权,但我认为您会有效地从完整屏障中获得类似的行为,这是其中的一部分联锁 CAS。
| 归档时间: |
|
| 查看次数: |
89 次 |
| 最近记录: |