C++ 联锁函数的变量是否必须在本机C++;

C++ 联锁函数的变量是否必须在本机C++;,c++,volatile,interlocked,C++,Volatile,Interlocked,以下两者之间是否有区别: __declspec(align(8)) long long variable1; //assume shared among threads InterlockedIncrement64(&variable1); 这是: __declspec(align(8)) long long volatile variable2;//assume shared among threads InterlockedIncrement64(&

以下两者之间是否有区别:

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

    __declspec(align(8)) long long volatile variable2;//assume shared among threads
    InterlockedIncrement64(&variable2);
我的猜测是,在这种情况下,它们的操作完全相同。我认为唯一的区别是,如果访问
variable1
时没有联锁功能,则不能保证它是当前值,而
variable2
则保证它是当前值。例如:

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
我的假设正确吗?
注意:我正在visual studio中使用/volatile:ms编译器选项

正确的方法是
std::atomic variable1--而不是依赖于MS/Windows的细节。回答这个问题有点复杂,因为
volatile
在MSVC++中是线程安全的,这不是一个可移植的假设。但您已经将可移植性抛在了
InterlockedIncrement