Say for example, I have an exclusive atomic-ops-based spin
lock implementation as below:
bool
TryLock(volatile TInt32 * pFlag)
{
return
!(AtomicOps::Exchange32(pFlag, 1) == 1);
}
void Lock
(volatile TInt32 * pFlag)
{
while
(AtomicOps::Exchange32(pFlag, 1) == 1) {
AtomicOps::ThreadYield();
}
}
void Unlock (volatile TInt32 *
pFlag)
{
*pFlag = 0; // is this ok? or here as well a atomicity is
needed for load and store
}
Where
AtomicOps::Exchange32
is implemented on windows using
InterlockedExchange
and on linux using
__atomic_exchange_n
.
No comments:
Post a Comment