- atomic[meta header]
- std[meta namespace]
- atomic_ref[meta class]
- function[meta id-type]
- cpp20[meta cpp]
T operator^=(T operand) const noexcept;
XOR演算を行う
以下と等価の式により、演算結果の値が返る:
return fetch_xor(operand) ^ operand;
- fetch_xor[link fetch_xor.md]
投げない
この関数は、atomic_ref
クラスの整数型に対する特殊化で定義される。
#include <iostream>
#include <atomic>
#include <bitset>
int main()
{
int value = 0b1011;
std::atomic_ref<int> x{value};
x ^= 0b1110;
std::cout << std::bitset<4>(value).to_string() << std::endl;
}
- to_string()[link /reference/bitset/bitset/to_string.md]
0101
#include <iostream>
#include <atomic>
#include <thread>
int main()
{
int ar[] = {1, 2, 3, 4, 5};
int hash{ar[0]};
// XORで配列のハッシュ値を並列に計算する。
// 複数スレッドでビット複合演算を呼んでも、
// 最終的に全てのスレッドでのビット複合演算が処理された値になる
std::thread t1 {[&hash, ar] {
std::atomic_ref{hash} ^= ar[1];
std::atomic_ref{hash} ^= ar[2];
}};
std::thread t2 {[&hash, ar] {
std::atomic_ref{hash} ^= ar[3];
std::atomic_ref{hash} ^= ar[4];
}};
t1.join();
t2.join();
std::cout << std::hex << hash << std::endl;
}
1
- C++20
- Clang: (9.0時点で実装なし)
- GCC: 10.1
- Visual C++: ??