std::atomic_flag 不提供加载或存储操作有什么缺点吗? (自旋锁示例)

Any disadvantages for std::atomic_flag not providing load or store operations? (Spin-lock example)

相比std::atomic_flag to an std::atomic_bool (aka std::atomic<bool>),我觉得std::atomic_flag只是界面更简单而已。它仅提供测试+设置和清除标志,而 std::atomic_bool 还为多个运算符提供重载。

我的一个问题是关于术语的:"load or store operations" 是什么意思?是不是说不能任意读取和修改一个std::atomic_flag的值?

此外,我想知道 std::atomic_bool 用于自旋锁时是否会更快?在我看来,std::atomic_flag 总是必须在自旋锁期间读取和写入:

while (my_atomic_flag.test_and_set()); // spin-lock

std::atomic_bool 只需要执行读取操作(假设原子 bool 是无锁实现的):

while (my_atomic_bool); // spin-lock

std::atomic_flag 是否比 std::atomic_bool 更有效,还是相反?自旋锁应该使用什么?

What is meant by "load or store operations"? Does it mean that it is not possible to arbitrarily read and modify a std::atomic_flag's value?

std::atomic_flag 不支持正常的 store/load 操作。
它是一种只能修改的类型; IE。如果不执行修改操作,则无法读取 std::atomic_flag 对象。
通常,std::atomic_flag 意味着作为其他操作的构建块。它的界面刻意简单;它是唯一保证原子无锁操作的原子类型。 它支持的操作有:

std::atomic_flag::clear()
std::atomic_flag::test_and_set()

有了它,您可以轻松构建自己的自旋锁(尽管通常不推荐):

class my_spinlock {
    std::atomic_flag flag = ATOMIC_FLAG_INIT;
public:

    void lock()
    {
        while(flag.test_and_set());
    }

    void unlock()
    {
        flag.clear();
    }
};

Furthermore, I am wondering, could a std::atomic_bool be faster when being used for a spin-lock? It seems to me that an std::atomic_flag always must read AND write during a spin-lock

好吧,问题是,自旋锁 总是 在获取锁时必须修改其状态。你不能不告诉别人就拿锁。
lock() 基于 std::atomic<bool> 的实现看起来非常相似:

while (flag.exchange(true)); 

基于std::atomic_flag的自旋锁是否更快?
在我的平台上,编译器为这两种类型发出相同的程序集,所以我会感到非常惊讶。