I have a multithreaded application (C++) where I need to increment/change a series of values. If I use a series of Interlocked operations, are they considered to be a single atomic operation ? Like in this example:
InterlockedIncrement(&value1);
InterlockedIncrement(&value2);
InterlockedExchange(&oldValue, newValue);
Or it would be better to us a lock to perform the synchronization ? Like this:
EnterCriticalSection(&cs);
value1++;
value2++;
oldValue = newValue;
LeaveCriticalSection(&cs);
I think a lock is required, but I'm not sure... it's very important that all the values to be either in the old state, or in the new one, together.
InterlockedIncrement
itself is an atomic operation but series of InterLockedIncrement
are not atomic together. If your requirement is to get the atomicity for series of operation then you can use critical section.