effil/src/spin-mutex.h
2017-01-21 22:39:58 +03:00

25 lines
407 B
C++

#pragma once
#include <atomic>
#include <thread>
namespace share_data {
class SpinMutex {
public:
void lock() noexcept {
while(lock_.test_and_set(std::memory_order_acquire)) {
std::this_thread::yield();
}
}
void unlock() noexcept {
lock_.clear(std::memory_order_release);
}
private:
std::atomic_flag lock_ = ATOMIC_FLAG_INIT;
};
} // share_data