Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
805 views
in Technique[技术] by (71.8m points)

winapi - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...