Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The usage of _InterlockedCompareExchange64

On MSDN, I see that

InterlockedCompareExchange64

requires parameters to be 64 aligned,

but for

_InterlockedCompareExchange64 

I see no such requirement for alignment, and it is mentioned to provide compiler intrinsic support for the InterlockedCompareExchange64.

So, does this means I can use _InterlockedCompareExchange64 without caring about alignment? 'Cause I don't quite know what alignment means here.

like image 233
Robert Bean Avatar asked Oct 31 '25 07:10

Robert Bean


2 Answers

I'm pretty certain that is a documentation mistake, rather than any direct difference between the two function - seeing as they result in exactly the same CMPXCHG8B instruction. [Assuming of course you are looking for it it to actually be atomic on an SMP system - but it's a pretty specialized instruction, so I doubt it has much other use].

like image 76
Mats Petersson Avatar answered Nov 01 '25 21:11

Mats Petersson


It is implied, the _underscore version is only available when _M_IA64 or _M_AMD64 is defined. In other words, when you target your program to a 64-bit processor.

You should use the non-underscore version in your code. When you target a 32-bit processor then the function is implemented in Windows and you'll get a safe version that works with a misaligned destination. But if you target a 64-bit processor then you'll automatically get the _underscore version and the intrinsic. Macro soup in WinBase.h takes care of it.

like image 29
Hans Passant Avatar answered Nov 01 '25 21:11

Hans Passant