From 8dab1e17c82bebf83639a8b387971bddca1e66d8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Tomic=CC=8Cevic=CC=81?= Date: Thu, 18 Jun 2015 14:55:04 +0200 Subject: [PATCH] created a fast locking mechanism using atomic_flag --- utils/sync/spinlock.hpp | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 utils/sync/spinlock.hpp diff --git a/utils/sync/spinlock.hpp b/utils/sync/spinlock.hpp new file mode 100644 index 000000000..bc46e63b8 --- /dev/null +++ b/utils/sync/spinlock.hpp @@ -0,0 +1,25 @@ +#ifndef MEMGRAPH_UTILS_SYNC_SPINLOCK_HPP +#define MEMGRAPH_UTILS_SYNC_SPINLOCK_HPP + +#include +#include + +class SpinLock +{ +public: + void acquire() + { + while(lock.test_and_set(std::memory_order_acquire)) + usleep(250); + } + + void release() + { + lock.clear(std::memory_order_release); + } + +private: + std::atomic_flag lock; +}; + +#endif