I have seen some repeated code (methods to be precise) where they are entering the critical section and then using InterlockedExchange…Does this make sense since I thought that this operation was infact atomic and would not require such synchronization?
{
EnterCricSectionLock lock (somelock);
InterlockedExchange(&somelong, static_cast<long>(newlongVal));
}
That is basically what there is…
A normal exchange is generally not atomic. It is however ok to do it while owning a mutex, if all other uses is protected by the same mutex. It is also ok to use an atomic exchange, if all other uses are atomic. The only logical reason I can think of to do an atomic exchange while owning the mutex, is that not all uses of this value is mutex protected.