memgraph/include/threading/sync/caslock.hpp
2016-08-10 09:39:02 +01:00

35 lines
612 B
C++

#pragma once
#include <atomic>
#include <unistd.h>
class CasLock
{
public:
void lock()
{
bool locked = false;
while(!lock_flag.compare_exchange_weak(locked, true,
std::memory_order_release,
std::memory_order_relaxed))
{
usleep(250);
}
}
void unlock()
{
lock_flag.store(0, std::memory_order_release);
}
bool locked()
{
return lock_flag.load(std::memory_order_relaxed);
}
private:
std::atomic<bool> lock_flag;
};