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

履歴 編集

function template
<memory>

std::atomic_exchange(C++11)

namespace std {
  template <class T>
  shared_ptr<T> atomic_exchange(shared_ptr<T>* p, shared_ptr<T> r);
}

概要

shared_ptrオブジェクトを、アトミックに入れ替える。

要件

p != nullptrであること。

戻り値

例外

投げない

#include <iostream>
#include <memory>

int main()
{
  std::shared_ptr<int> a(new int(1));
  std::shared_ptr<int> b(new int(2));

  std::shared_ptr<int> prev_state = std::atomic_exchange(&a, b);

  std::cout << *a << std::endl;
  std::cout << *prev_state << std::endl;
}

出力

2
1

バージョン

言語

  • C++11

処理系

参照