使用 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;
};