InterlockedIncrement vs InterlockedIncrementAcquire vs InterlockedIncrementNoFence [duplicate]

断了今生、忘了曾经 提交于 2020-01-06 08:21:32

问题


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.

Thanks.


回答1:


Going off the documentation,

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 tries to use "acquire semantics" if supported by your system, if not, it falls back to InterlockedIncrement. Going off of that blog post,

Acquire semantics prevent memory reordering of the read-acquire with any read or write operation which follows it in program order

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).

Finally, InterlockedIncrementNoFence generates no memory barrier -- it's completely unchecked, and it's possible that it will cause you issues with sequential consistency.



来源:https://stackoverflow.com/questions/25487373/interlockedincrement-vs-interlockedincrementacquire-vs-interlockedincrementnofen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!