• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function template
    <atomic>

    std::atomic_fetch_or

    namespace std {
      template <class T>
      T
        atomic_fetch_or(
          volatile atomic<T>* object,
          T operand) noexcept;                              // (1) C++11
      template <class T>
      T
        atomic_fetch_or(
          volatile atomic<T>* object,
          typename atomic<T>::value_type operand) noexcept; // (1) C++17
    
      template <class T>
      T
        atomic_fetch_or(
          atomic<T>* object,
          T operand) noexcept;                              // (2) C++11
      template <class T>
      T
        atomic_fetch_or(
          atomic<T>* object,
          typename atomic<T>::value_type operand) noexcept; // (2) C++17
      template <class T>
      constexpr T
        atomic_fetch_or(
          atomic<T>* object,
          typename atomic<T>::value_type operand) noexcept; // (2) C++26
    }
    

    概要

    アトミックにOR演算を行う

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

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

    効果

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

    戻り値

    変更前の値が返される

    例外

    投げない

    備考

    符号付き整数型に対しては、2の補数表現による演算が行われ、未定義動作はない。アドレス型に関しては結果として未定義アドレスになる場合があるが、それ以外の未定義動作はない。

    #include <iostream>
    #include <atomic>
    #include <bitset>
    
    int main()
    {
      int a = 0x0b;
      int b = 0x0e;
    
      std::atomic<int> x(a);
    
      std::atomic_fetch_or(&x, 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++11

    処理系

    関連項目

    参照