问题描述
有人可以解释这三个原子操作之间的区别吗?
Can someone explain the difference betweeen these three atomic operations?
- InterlockedIncrement
- InterlockedIncrementAcquire
- InterlockedIncrementNoFence
除了我不理解的用途获得共鸣"外,我似乎找不到任何有关其含义的文档.
I can't seem to find any documentation on what they mean other than "uses acquire symantecs" which I don't understand.
谢谢.
推荐答案
关闭文档,
InterlockedIncrement 为每个呼叫生成完全内存屏障".内存屏障是对CPU的特殊指令,可防止其像往常一样对操作进行重新排序-例如,加载操作可能非常昂贵,因此给定一系列的操作流,例如添加到A,添加到A,加载B" ,添加到B",CPU会尝试将其重新排序为加载B,添加到A,添加到A,添加到B",以便B的加载在需要之前有时间完成.
InterlockedIncrement "generates a full memory barrier" for every call. Memory barriers are special instructions to your CPU that prevent it from reordering operations like it usually does -- for instance, load operations can be very expensive, so given a stream of ops that look like "add to A, add to A, load B, add to B", the CPU will attempt to reorder it as "load B, add to A, add to A, add to B" so that the load of B has time to complete before it's needed.
但是,这可能会破坏并行程序中的逻辑,因此有时需要使用内存屏障.它们很昂贵:它们的成本往往接近缓存未命中的成本.
However, this can destroy logic in parallel programs, so sometimes memory barriers are necessary. They're expensive: they tend to cost around as much as a cache miss.
InterlockedIncrementAcquire 如果系统支持,则尝试使用"获取语义" ,它回落到InterlockedIncrement.结束该博客文章
InterlockedIncrementAcquire tries to use "acquire semantics" if supported by your system, if not, it falls back to InterlockedIncrement. Going off of that blog post,
因此,获取语义是一种有限的,便宜的内存屏障,仅在某些情况下才有用(很明显,当只涉及读取时).
So acquire semantics are a limited, less-expensive kind of memory barrier that's only useful in certain situations (when only reads are involved, evidently).
最后, InterlockedIncrementNoFence 不会产生内存障碍-完全不受检查,并且有可能导致顺序问题的发生.
Finally, InterlockedIncrementNoFence generates no memory barrier -- it's completely unchecked, and it's possible that it will cause you issues with sequential consistency.
这篇关于InterlockedIncrement与InterlockedIncrementAcquire与InterlockedIncrementNoFence的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!