Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between InterlockedCompareExchange Release() and Acquire()?

What's the difference between InterlockedCompareExchangeRelease() and InterlockedCompareExchangeAcquire()?

When I try to learn the synchronization functions with WIN32 API, I find there are two functions named differently but seems to do the same thing:

LONG __cdecl InterlockedCompareExchangeRelease(
  __inout  LONG volatile *Destination,
  __in     LONG Exchange,
  __in     LONG Comparand
);

and

LONG __cdecl InterlockedCompareExchangeAcquire(
  __inout  LONG volatile *Destination,
  __in     LONG Exchange,
  __in     LONG Comparand
);

I check the MSDN, it says those functions are:

Performs an atomic compare-and-exchange operation on the specified values. The function compares two specified 32-bit values and exchanges with another 32-bit value based on the outcome of the comparison.

but for InterlockedCompareExchangeAcquire(),

The operation is performed with acquire memory access semantics.

and for InterlockedCompareExchangeRelease(),

The exchange is performed with release memory access semantics.

So I'm curious about the difference between these two functions. When to use the acquire memory access semantics or release memory access semantics? Are there any examples?

Thanks!

like image 219
Celebi Avatar asked Mar 19 '12 02:03

Celebi


2 Answers

The plain version uses a full barrier while the suffixed versions only deals with loads or stores, this can be faster on some CPUs (Itanium-based processors etc)

MSDN has a article about Acquire and Release Semantics and the Interlocked* API as well as this great blog post. The Linux memory barrier documentation might also be useful...

like image 65
Anders Avatar answered Nov 10 '22 12:11

Anders


I found this and this on MSDN:

Acquire memory semantics specify that the memory operation being performed by the current thread will be visible before any other memory operations are attempted. Release memory semantics specify that the memory operation being performed by the current thread will be visible after all other memory operations have been completed. These semantics allow you to force memory operations to be performed in a specific order. Use acquire semantics when entering a protected region and release semantics when leaving it.

like image 45
Jacob Seleznev Avatar answered Nov 10 '22 12:11

Jacob Seleznev