Skip to main content
GameDev.net gamedev.net
🔒 Locked

Atomic Compare and Swap

Started by beebs1 Jun 8, 2009 at 11:28 AM 3 replies 2.5k views
Original Post
beebs1
beebs1
Hiya, I'm using the InterlockedCompareExchangePointer() function on Windows for some lock-free data structures. There are a couple of things in the documentation I don't fully understand. Can anyone tell me the difference between Acquire and Release semantics, and when each should be used? MSDN says they should be used in performance-critical applications. Also, what is the difference between a function and a compiler intrinsic? Many thanks for any help [smile] Cheers, James
samoth
samoth
Release semantics mean that [whatever operation] is realized, globally, after other pending operations have finished.
Acquire semantics mean that [whatever operation] will complete and become globally visible before any other operations started after it become visible, regardless of out-of-order execution, write combining and other effects.
Sneftel
Sneftel
Quote:
Original post by beebs1
Can anyone tell me the difference between Acquire and Release semantics, and when each should be used?

It has to do with synchronization. Acquire semantics mean that the atomic operation you're doing is the start of something unsafe, and that before the function returns, that atomic operation had better actually have completed. Release semantics mean that you've just finished doing the unsafe stuff, and that it had all better be complete before you do the atomic operation. So basically, use Acquire when it's the first inter-thread thing you're doing in a block, and use Release when it's the last inter-thread thing you're doing in a block. The key thing is, each of those "performance critical" versions is identified by what it DOESN'T do, namely barrier before or barrier after. So if neither of those describes your situation, just use regular InterlockedCompareExchange, which has both Acquire AND Release semantics, and is slower because it does both.
Quote:
Also, what is the difference between a function and a compiler intrinsic

A compiler intrinsic is processed directly by the compiler and will generally turn into tightly optimized inline assembly, rather than a function call.
beebs1
beebs1
Perfect explanations - thank you [smile].

Ratings++
zeux
zeux
Note that you don't need to differentiate between Acquire/Release/AcqRel semantics unless your platform indeed has different performance characteristics for these operations. On x86 and x86-64 there is no difference, on Itanium there is. Though of course understanding the difference and potential memory ordering problems is very useful if your target has memory ordering guarantees that are weaker than x86 ones.
beebs1
beebs1
Interesting, thanks.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.