created a fast locking mechanism using atomic_flag
This commit is contained in:
25
utils/sync/spinlock.hpp
Normal file
25
utils/sync/spinlock.hpp
Normal file
@@ -0,0 +1,25 @@
|
||||
#ifndef MEMGRAPH_UTILS_SYNC_SPINLOCK_HPP
|
||||
#define MEMGRAPH_UTILS_SYNC_SPINLOCK_HPP
|
||||
|
||||
#include <atomic>
|
||||
#include <unistd.h>
|
||||
|
||||
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
|
||||
Reference in New Issue
Block a user