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

246 Views Asked by At

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

2

There are 2 best solutions below

5
On BEST ANSWER

Simply use a type-cast:

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

In your link, casting a unigned something to types of different sizes
(and/or floting point stuff) is explained.
Casting only between signed and unigned of the same type
should be possible without any problems.