It's not clear what you're asking, but InterlockedExchange
atomically does the following:
- reads the pre-existing value of the variable
- writes the variable
Note that only one variable is involved in the operation, along with two temporaries (the value being written, and the prior value returned). Whereas "swap" usually means writing two variables, such that each has the value which pre-existed in the other. That would be:
byte[] src;
byte[] dest;
lock(synchLock)
{
var temp = dest;
dest = src;
src = temp;
}
InterlockedExchange
cannot be used to implement lock-less swap with atomic effect on both variables.