Reputation: 9278
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.
Upvotes: 2
Views: 120
Reputation: 129374
Roughly yes. The lock or
on the first case makes sure that the data has been updated before any other CPU can read any more memory, in the second case, the xchg
instruction has an implicit lock, so all other processor (cores) will have to "release" their value of *ptr before your processor can update the value.
Upvotes: 2