使用 C++11 原子类型 std::atomic_flag
实现的自旋锁:
#include <atomic>
class Spinlock {
public:
Spinlock(): flag(ATOMIC_FLAG_INIT) {}
void lock() {
while (flag.test_and_set(std::memory_order_acquire));
}
void unlock() {
flag.clear(std::memory_order_release);
}
private:
std::atomic_flag flag;
};
标签:11,std,C++,flag,atomic,自旋
From: https://www.cnblogs.com/BuzzWeek/p/17739788.html