- memory[meta header]
- std[meta namespace]
- function template[meta id-type]
- cpp11[meta cpp]
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)- atomic_compare_exchange_weak_explicit[link atomic_compare_exchange_weak_explicit.md]
- memory_order_seq_cst[link /reference/atomic/memory_order.md]
投げない
#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;
}- std::atomic_compare_exchange_weak[color ff0000]
- std::atomic_load[link atomic_load.md]
3
- C++11
- Clang, C++11 mode: 3.3
- GCC, C++11 mode: 5.0
- ICC: ??
- Visual C++: 2012, 2013