• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <atomic>

    std::atomic::fetch_or

    T
      fetch_or(T operand,
               memory_order order = memory_order_seq_cst
               ) volatile noexcept;                      // (1) C++11
    
    T
      fetch_or(T operand,
               memory_order order = memory_order_seq_cst
               ) noexcept;                               // (2) C++11
    constexpr T
      fetch_or(T operand,
               memory_order order = memory_order_seq_cst
               ) noexcept;                               // (2) C++26
    

    概要

    OR演算を行う

    テンプレートパラメータ制約

    • (1), (2) :
      • C++17 : std::atomic<T*>の場合、型Tがオブジェクト型であること。型Tvoid*や関数ポインタであってはならない
    • (1) :
      • C++20 : atomic<T>::is_always_lock_freetrueであること

    効果

    orderで指定されたメモリオーダーにしたがって、現在の値にoperandをORした値でアトミックに置き換える

    戻り値

    変更前の値が返される

    例外

    投げない

    備考

    • この関数は、atomicクラスの整数型に対する特殊化で定義される。
    • 符号付き整数型に対しては、符号なし整数型に変換されたかのようにしたあと演算が行われ、結果は符号付き整数型になる。未定義動作はない

    基本的な使い方

    #include <iostream>
    #include <atomic>
    #include <bitset>
    
    int main()
    {
      int a = 0x0b;
      int b = 0x0e;
    
      std::atomic<int> x(a);
    
      x.fetch_or(b);
    
      std::cout << std::bitset<4>(a).to_string() << std::endl;
      std::cout << std::bitset<4>(b).to_string() << std::endl;
      std::cout << std::bitset<4>(x.load()).to_string() << std::endl;
    }
    

    出力

    1011
    1110
    1111
    

    複数スレッドからビット複合演算を行う例 (C++14)

    #include <iostream>
    #include <atomic>
    #include <thread>
    #include <bitset>
    
    int main()
    {
      std::atomic<int> x{0b0110};
    
      // 複数スレッドでビット複合演算を呼んでも、
      // 最終的に全てのスレッドでのビット複合演算が処理された値になる
      std::thread t1 {[&x] {
        x.fetch_or(0b0001);
      }};
      std::thread t2 {[&x] {
        x.fetch_or(0b1000);
      }};
    
      t1.join();
      t2.join();
    
      int value = x.load();
      std::cout << std::bitset<4>(value).to_string() << std::endl;
    }
    

    出力

    1111
    

    バージョン

    言語

    • C++11

    処理系

    関連項目

    参照