winapivisual-studio-2012atomic-swap

What's the correct way to atomically exchange two unsigned 32-bit variables (ULONG)?


I found the InterlockedExchange function which allows me to exchange two signed 32-bit variables (LONG).

But, what is the correct way to atomically exchange two unsigned 32-bit variables (ULONG) under Windows?

I do not see an obvious way to do that using the functions provided by Microsoft.

(Considering that Microsoft also tells me that the result of converting unsigned integers to signed integers is implementation-defined in some cases.)


Solution

  • Simply use a type-cast:

    ULONG value1, value2;
    InterlockedExchange((LPLONG)&value2, (LONG)value1);