I cannot use C++11 atomics here
I have a doubt about my use of InterlockedCompareExchange() in order to 'atomically' read a variable.
I asked a question about this here, but what I was doing there was different. The "exchange" and "comperand" parameters (2nd and 3rd) were 'hard coded' values, i.e. not read from a variable.
Please consider this:
// Copy the connect time
DWORD dwConnectTime = InterlockedCompareExchange(&msgInfo.m_dwConnectTime,
msgInfo.m_dwConnectTime,
msgInfo.m_dwConnectTime);
This is intended to swap the value of msgInfo.m_dwConnectTime with the current value of msgInfo.m_dwConnectTime, provided the current value of msgInfo.m_dwConnectTime is msgInfo.m_dwConnectTime. The previous value of msgInfo.m_dwConnectTime is then returned, which is what I rely on to 'copy' the value.
It has just dawned on me though that the reads of msgInfo.m_dwConnectTime for the second and third parameters themselves are not guaranteed to be atomic. Thus, is this code incorrect hence I need to use a locking primitive to copy msgInfo.m_dwConnectTime?