Namespaces
Variants

std:: atomic_flag

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
atomic_flag
(C++11)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
ヘッダーで定義 <atomic>
class atomic_flag ;
(C++11以降)

std::atomic_flag はアトミックなブーリアン型です。 std::atomic のすべての特殊化とは異なり、ロックフリーであることが保証されています。 std:: atomic < bool > とは異なり、 std::atomic_flag はロード操作やストア操作を提供しません。

メンバー関数

atomic_flagを構築する
(公開メンバ関数)
[deleted]
代入演算子 (削除済み)
(公開メンバ関数)
フラグをアトミックに false に設定する
(公開メンバ関数)
フラグをアトミックに true に設定し、以前の値を取得する
(公開メンバ関数)
(C++20)
フラグの値をアトミックに返す
(公開メンバ関数)
(C++20)
通知されるまでスレッドをブロックし、アトミック値が変更されるのを待つ
(公開メンバ関数)
(C++20)
アトミックオブジェクトを待機している少なくとも1つのスレッドに通知する
(公開メンバ関数)
(C++20)
アトミックオブジェクトを待機しているすべてのスレッドに通知する
(公開メンバ関数)

spinlock ミューテックスのデモは、ユーザースペースで 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でブロックされているスレッドを1つ通知する
(関数)
atomic_flag_waitでブロックされているすべてのスレッドを通知する
(関数)
std::atomic_flag false に初期化する
(マクロ定数)
Cドキュメント for atomic_flag