libxr 1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
spin_lock.hpp
1#pragma once
2
3#include <atomic>
4
5namespace LibXR
6{
7
17{
18 private:
19 std::atomic_flag atomic_flag_ = ATOMIC_FLAG_INIT;
20
21 public:
33 {
34 return !atomic_flag_.test_and_set(std::memory_order_acquire);
35 }
36
45 {
46 while (atomic_flag_.test_and_set(std::memory_order_acquire))
47 {
48 }
49 }
50
57 void Unlock() noexcept { atomic_flag_.clear(std::memory_order_release); }
58};
59
60} // namespace LibXR
轻量级自旋锁实现 / Lightweight spinlock implementation
Definition spin_lock.hpp:17
void Lock() noexcept
阻塞直到获取锁 / Blocks until the lock is acquired
Definition spin_lock.hpp:44
bool TryLock() noexcept
尝试获取锁 / Attempts to acquire the lock
Definition spin_lock.hpp:32
void Unlock() noexcept
释放锁 / Releases the lock
Definition spin_lock.hpp:57
std::atomic_flag atomic_flag_
自旋锁标志 / Spinlock flag
Definition spin_lock.hpp:19
LibXR Color Control Library / LibXR终端颜色控制库
constexpr auto min(T1 a, T2 b) -> typename std::common_type< T1, T2 >::type
计算两个数的最小值