Windows API offers InterlockedExchange, which sets a value in memory atomically. Using only GCC intrinsics, I’d like to create an equivalent of that function. Would setting the value and then calling a memory barrier be sufficient (see the code below) ?
template <typename T>
T InterlockedExchange(volatile T& _data, T _value)
{
const T oldValue = _data;
_data = _value;
__sync_synchronize();
return oldValue;
}
Thank you.
EDIT: The proposed snippet is NOT a correct solution to the problem, as it is clearly not atomic (but, well, I had to give a try at least).
Use
__sync_val_compare_and_swap__sync_lock_test_and_set, not__sync_synchronize.This has exactly the same function as InterlockedExchange.
Something like this (untested code!):
EDIT:
Oi, I read wrong, you wanted InterlockedExchange, not InterlockedCompareExchange … so that is
__sync_lock_test_and_set(the name is a misleading Intel-nomer, but it’s exactly what you want).See here, bottom of the page.