43 lines
569 B
C++
43 lines
569 B
C++
/*
|
||
* Spinlock_mutex.hh
|
||
*
|
||
* Created on: 22 <20><><EFBFBD>. 2020 <20>.
|
||
* 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);
|
||
|
||
};
|
||
};
|
||
|
||
}
|