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

履歴 編集

function template
<atomic>

std::atomic_fetch_xor(C++11)

namespace std {
  template <class T>
  T atomic_fetch_xor(
      volatile atomic<T>* object,
      T operand) noexcept;                              // (1) C++11

  template <class T>
  T atomic_fetch_xor(
      volatile atomic<T>* object,
      typename atomic<T>::value_type operand) noexcept; // (1) C++17

  template <class T>
  T atomic_fetch_xor(
      atomic<T>* object,
      T operand) noexcept;                              // (2) C++11

  template <class T>
  T atomic_fetch_xor(
      atomic<T>* object,
      typename atomic<T>::value_type operand) noexcept; // (2) C++17
}

概要

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

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

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

効果

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

戻り値

変更前の値が返される

例外

投げない

備考

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

#include <iostream>
#include <atomic>
#include <bitset>

int main()
{
  int a = 0x0b;
  int b = 0x0e;

  std::atomic<int> x(a);

  std::atomic_fetch_xor(&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
0101

バージョン

言語

  • C++11

処理系

関連項目

参照