4

Is there a difference between :

    __declspec(align(8)) long long variable1; //assume shared among threads
    InterlockedIncrement64(&variable1);

And this:

    __declspec(align(8)) long long volatile variable2;//assume shared among threads
    InterlockedIncrement64(&variable2);

My guess would be that they operate exactly the same in this scenario. I think the only difference would be that if variable1 is accessed without an interlocked function it is not guaranteed to be a current value while variable2is guaranteed to be a current value. Example:

long long x = variable1; //Not guaranteed to be the most recent value when accessed
long long y = variable2; //Guaranteed to be the most recent value when accessed

Am I correct in my assumptions? Note: I am using /volatile:ms compiler option in visual studio

Tas
  • 7,023
  • 3
  • 36
  • 51
David Jones
  • 149
  • 13
  • 2
    The proper way would be `std::atomic variable1;` - http://en.cppreference.com/w/cpp/atomic - rather than rely on MS/Windows specifics. – Jesper Juhl Aug 10 '16 at 05:51
  • A bit of a complication in answering this is that `volatile` is threadsafe in MSVC++, which is not a portable assumption. But you already left portability behind with `InterlockedIncrement` – MSalters Aug 10 '16 at 07:27

0 Answers0