使用 C++11 原子类型 `std::atomic_flag` 实现的自旋锁

使用 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;
};
posted @ 2023-10-02 11:02  BuzzWeek  阅读(61)  评论(0编辑  收藏  举报