Sign in to follow this  
beebs1

Atomic Compare and Swap

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this