命名空間
變體
動作

std::atomic_flag

出自 cppreference.com
< cpp‎ | atomic
 
 
並行支援函式庫
執行緒
(C++11)
(C++20)
this_thread 命名空間
(C++11)
(C++11)
(C++11)
協作式取消
互斥 (Mutual exclusion)
(C++11)
通用鎖管理
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
條件變數
(C++11)
旗標 (Semaphores)
閂鎖 (Latches) 與屏障 (Barriers)
(C++20)
(C++20)
期約 (Futures)
(C++11)
(C++11)
(C++11)
(C++11)
安全回收
(C++26)
風險指標 (Hazard Pointers)
原子型別
(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 的所有特化版本不同,它保證是無鎖(lock-free)的。與 std::atomic<bool> 不同,std::atomic_flag 不提供載入(load)或儲存(store)操作。

[編輯] 成員函數

建構 atomic_flag
(公開成員函式)
[已刪除]
賦值運算子(已刪除)
(公開成員函式)
以原子方式將旗標設為 false
(公開成員函數) [編輯]
以原子方式將旗標設為 true 並取得其先前的值
(公開成員函數) [編輯]
(C++20)
原子地回傳旗標的值
(公開成員函數) [編輯]
(C++20)
阻塞執行緒直到收到通知且原子值發生變化
(公開成員函式) [編輯]
通知至少一個在該原子物件上等待的執行緒
(公開成員函式) [編輯]
通知所有在該原子物件上阻塞等待的執行緒
(公開成員函式) [編輯]

[編輯] 範例

可以使用 atomic_flag 在使用者空間實作 自旋鎖(spinlock) 互斥鎖。請注意,在實務上,自旋鎖互斥鎖具有 極大的爭議性

#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
English Deutsch 日本語 中文(简体) 中文(繁體)