简体   繁体   中英

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

Simply use a type-cast:

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM