命名空间
变体
操作

std::atomic_flag

来自 cppreference.cn
< cpp‎ | atomic
 
 
并发支持库
线程
(C++11)
(C++20)
this_thread 命名空间
(C++11)
(C++11)
(C++11)
协作式取消
互斥
(C++11)
通用锁管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
条件变量
(C++11)
信号量
闩锁和屏障
(C++20)
(C++20)
期物
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
危害指针
原子类型
(C++11)
(C++20)
atomic_flag
(C++11)
原子类型的初始化
(C++11)(在 C++20 中弃用)
(C++11)(在 C++20 中弃用)
内存顺序
(C++11)(在 C++26 中弃用)
原子操作的自由函数
原子标志的自由函数
 
 
定义于头文件 <atomic>
class atomic_flag;
(自 C++11 起)

std::atomic_flag 是一个原子布尔类型。与 std::atomic 的所有特化不同,它保证是无锁的。与 std::atomic<bool> 不同,std::atomic_flag 不提供加载或存储操作。

[编辑] 成员函数

构造一个 atomic_flag
(公共成员函数)
[已删除]
赋值运算符 (已删除)
(公共成员函数)
原子地将标志设置为 false
(公共成员函数) [编辑]
原子地将标志设置为 true 并获取其先前的值
(公共成员函数) [编辑]
(C++20)
原子地返回标志的值
(公共成员函数) [编辑]
(C++20)
阻塞线程直到被通知且原子值发生更改
(公共成员函数) [编辑]
通知至少一个等待原子对象的线程
(公共成员函数) [编辑]
通知所有阻塞等待原子对象的线程
(公共成员函数) [编辑]

[编辑] 示例

一个 自旋锁 互斥锁演示可以使用 atomic_flag 在用户空间中实现。请注意,自旋锁互斥锁在实践中是 极其可疑的

#include <atomic>
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
 
class mutex
{
    std::atomic_flag m_{};
 
  public:
    void lock() noexcept
    {
        while (m_.test_and_set(std::memory_order_acquire))
#if defined(__cpp_lib_atomic_wait) && __cpp_lib_atomic_wait >= 201907L
            // Since C++20, locks can be acquired only after notification in the unlock,
            // avoiding any unnecessary spinning.
            // Note that even though wait guarantees it returns only after the value has
            // changed, the lock is acquired after the next condition check.
            m_.wait(true, std::memory_order_relaxed)
#endif
                ;
    }
    bool try_lock() noexcept
    {
        return !m_.test_and_set(std::memory_order_acquire);
    }
    void unlock() noexcept
    {
        m_.clear(std::memory_order_release);
#if defined(__cpp_lib_atomic_wait) && __cpp_lib_atomic_wait >= 201907L
        m_.notify_one();
#endif
    }
};
 
static mutex m;
 
static int out{};
 
void f(std::size_t n)
{
    for (std::size_t cnt{}; cnt < 40; ++cnt)
    {
        std::lock_guard lock{m};
        std::cout << n << ((++out % 40) == 0 ? '\n' : ' ');
    }
}
 
int main()
{
    std::vector<std::thread> v;
    for (std::size_t n{}; n < 10; ++n)
        v.emplace_back(f, n);
    for (auto &t : v)
        t.join();
}

可能的输出

0 1 1 2 0 1 3 2 3 2 0 1 2 3 2 3 0 1 3 2 0 1 2 3 2 3 0 3 2 3 2 3 2 3 1 2 3 0 1 3
2 3 2 0 1 2 3 0 1 2 3 2 0 1 2 3 0 1 2 3 2 3 2 3 2 0 1 2 3 2 3 0 1 3 2 3 0 2 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 3 2 0 2 3 2 3 2 3 2 3 2 3 0 3
2 3 0 3 0 3 2 3 0 3 2 3 2 3 0 2 3 0 3 2 0 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4
5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5 5
6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6 6
7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7 7
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9

[编辑] 参见

原子地将标志设置为 true 并返回其先前的值
(函数) [编辑]
原子地将标志的值设置为 false
(函数) [编辑]
阻塞线程直到被通知且标志发生更改
(函数) [编辑]
通知在 atomic_flag_wait 中阻塞的线程
(函数) [编辑]
通知所有在 atomic_flag_wait 中阻塞的线程
(函数) [编辑]
std::atomic_flag 初始化为 false
(宏常量) [编辑]
C 文档 关于 atomic_flag