namespace std {
template<class T>
bool atomic_compare_exchange_weak(
shared_ptr<T>* p, shared_ptr<T>* expected, shared_ptr<T> desired);
}
概要
弱い比較で、アトミックにshared_ptr
オブジェクトを入れ替える。
要件
p != nullptr
であること。expected != nullptr
であること。
戻り値
atomic_compare_exchange_weak_explicit(
p,
expected,
desired,
memory_order_seq_cst,
memory_order_seq_cst)
例外
投げない
例
#include <iostream>
#include <memory>
int main()
{
std::shared_ptr<int> p(new int(1));
std::shared_ptr<int> ps = p;
std::shared_ptr<int> q(new int(3));
while (!std::atomic_compare_exchange_weak(&p, &ps, q)) {}
std::shared_ptr<int> result = std::atomic_load(&p);
std::cout << *result << std::endl;
}
出力
3
バージョン
言語
- C++11
処理系
- Clang: 3.3
- GCC: 5.0
- ICC: ??
- Visual C++: 2012, 2013