43 lines
581 B
C++
43 lines
581 B
C++
/*
|
|
* Spinlock_mutex.hh
|
|
*
|
|
* Created on: 22 пїЅпїЅпїЅ. 2020 пїЅ.
|
|
* Author: _aam
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
|
|
namespace logging {
|
|
|
|
class SpinlockMutex {
|
|
|
|
std::atomic_flag spinlock_flag;
|
|
unsigned long tick_tok;
|
|
|
|
public:
|
|
|
|
SpinlockMutex() : tick_tok(0) { spinlock_flag.clear(); };
|
|
|
|
~SpinlockMutex() {
|
|
|
|
unlock();
|
|
|
|
}
|
|
|
|
void lock() {
|
|
|
|
while( spinlock_flag.test_and_set(std::memory_order_acquire) )
|
|
tick_tok++;
|
|
|
|
}
|
|
void unlock() {
|
|
|
|
spinlock_flag.clear(std::memory_order_release);
|
|
|
|
};
|
|
};
|
|
|
|
}
|