The code provided below highlights a potential issue with using the InterlockedCompareExchange128
function in C++. The problem is that there is no atomic get/set 128 function available, but they can be synthesized from InterlockedCompareExchange128
.
However, there are incorrect forms that can work in non-race conditions, which can mislead developers into thinking their code is correct. The first incorrect form is the Get
function, which uses a non-atomic read of *p
and can cause problems if p
happens to match the input value. The second incorrect form is the Set
function, which writes the desired value through the last parameter, which is not atomic.
To solve these issues, the correct forms of Get
and Set
functions have been provided in the code. In the correct form of the Get
function, the compare will match, and the same value will be written back atomically. In the correct form of the Set
function, the code