void f1(volatile int* ptr, int value)
{
*ptr = value;
lock or DWORD PTR [rsp], 0; // MemoryBarrier()
}
void f2(volatile int* ptr, int value)
{
xchg DWORD PTR [ptr], value; // InterlockedExchange(ptr, value);
}
Equivalent in terms of semantics. Apparently xchg is locked whether or not the lock prefix is specified or not.
Edit: I’m using VS2010 currently but will probably port to VS2012 where I believe the compiler semantics regarding volatile have changed again.
Roughly yes. The
lock oron the first case makes sure that the data has been updated before any other CPU can read any more memory, in the second case, thexchginstruction has an implicit lock, so all other processor (cores) will have to “release” their value of *ptr before your processor can update the value.