• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

    最終更新日時(UTC):
    が更新

    履歴 編集

    function
    <atomic>

    std::atomic_flag_test_and_set

    namespace std {
      bool
        atomic_flag_test_and_set(volatile atomic_flag* object) noexcept; // (1) C++11
    
      bool
        atomic_flag_test_and_set(atomic_flag* object) noexcept;          // (2) C++11
      constexpr bool
        atomic_flag_test_and_set(atomic_flag* object) noexcept;          // (2) C++26
    }
    

    概要

    アトミックにテストしてフラグを立てる

    効果

    memory_order_seq_cstのメモリオーダーにしたがって、アトミックにtrue値を書き込む。この操作はread-modify-write操作である。

    戻り値

    変更前の値

    例外

    投げない

    #include <iostream>
    #include <atomic>
    
    int main()
    {
      std::atomic_flag x = ATOMIC_FLAG_INIT;
    
      std::cout << std::boolalpha;
      {
        // 値をtrueに設定する(変更前の値はfalse)
        bool result = std::atomic_flag_test_and_set(&x);
        std::cout << result << std::endl;
      }
      {
        // 値をtrueに設定する(変更前の値はtrue)
        bool result = std::atomic_flag_test_and_set(&x);
        std::cout << result << std::endl;
      }
    }
    

    出力

    false
    true
    

    バージョン

    言語

    • C++11

    処理系

    参照