I am trying to use the MS VC++ Intrinsic InterlockedCompareExchange128 function.
As a hello-world, I am trying to compare a 16byte address with itself, and replace it with something else. This compiles, but it’s not working – the address is not exchanged with new values. The const_cast is used to make it compile (otherwise it cries for not being able to cast volatile).
typedef struct t_node
{
volatile __int64 arr[2];
}node;
int main()
{
node *a = new node();
a->arr[0] = 100;
a->arr[1] = 1;
__int64 i = 200;
__int64 j = 500;
char r = _InterlockedCompareExchange128(a->arr, i,j, const_cast<__int64*>(&a->arr[0]));
cout<<endl<<"Interlocked Compare Res: "<<r;
cin.get();
return 0;
}
From the documentation:
Thus, what happens is in pseudocode:
What happens when
Destination == ComparandResult(your case) is:Which is a nop.
In addition there is a note for that in the same page: