摘要
原型: struct atomic_flag;
Atomic flags是boolean atomic 对象, 支持两种操作, test-and-set 和 clear.
Atomic flags是 lock-free(原子操作), 是唯一被保证的lock-free 原子操作的.
Constructor
atomic_flag() noexcept = default;
atomic_flag (const atomic_flag& T) = delete; // atomic_flag不能被 copied/moved
atomic_flag的状态 没有在 构造函数中指定, 除非 显示地被 初始化为 ATOMIC_FLAG_INIT.
test_and_set
函数原型为:
bool test_and_set (memory_order sync = memory_order_seq_cst) volatile noexcept;
bool test_and_set (memory_order sync = memory_order_seq_cst) noexcept;
1. 如果atomic flags设置过, 则 test_and_set()返回true
2. 否则, 返回false
test_and_set() 函数是 atomic原子操作, read-modify-write原子操作.
memory order是对atomic操作的一直约束.
#include <memory>
#include <iostream>
#include <mutex> // std::mutex
#include <atomic> // std::atomic, std::at