This is a Test and Test-And-Set implementation of Spinlock. The following is the code for lock
method.
struct ttas_lock {
...
void lock() {
for (;;) {
if (!lock_.exchange(true, std::memory_order_acquire)) {
break;
}
while (lock_.load(std::memory_order_relaxed)); // <--- spin
}
}
...
};
It spins in a while loop to check if the lock has been released before acquiring it. So that it can reduce cache coherency traffic.
However, my question is:
Since it loads lock_
with relaxed order, which doesn't sync with the release operation, i.e. unlock
method, is it possible that it never loads a false
value or it gets high latency before loading a false
value?