Skip to content

Latest commit

 

History

History
97 lines (69 loc) · 2.98 KB

File metadata and controls

97 lines (69 loc) · 2.98 KB

atomic_compare_exchange_strong_explicit

  • memory[meta header]
  • std[meta namespace]
  • function template[meta id-type]
  • cpp11[meta cpp]
namespace std {
  template<class T>
  bool atomic_compare_exchange_strong_explicit(
         shared_ptr<T>* p, shared_ptr<T>* expected, shared_ptr<T> desired,
         memory_order success, memory_order failure);
}
  • memory_order[link /reference/atomic/memory_order.md]

概要

メモリオーダーを指定して、強い比較で、アトミックにshared_ptrオブジェクトを入れ替える。

要件

効果

現在の値pexpectedが等しければ、*pdesiredで置き換え、そうでなければ*p*expectedで置き換える。

等しい場合はsuccessメモリオーダー、そうでなければfailureメモリオーダーに従って、アトミックに値の置き換えが行われる。

戻り値

*p*expectedが等しければtrue、そうでなければfalseを返す。

例外

投げない

備考

等値比較は、2つのshared_ptrオブジェクトが同じポインタを保持し、リソースを共有していればtrueとなる。

#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));
  std::atomic_compare_exchange_strong_explicit(
    &p, &ps, std::move(q),
    std::memory_order_acquire,
    std::memory_order_acquire);

  std::shared_ptr<int> result = std::atomic_load(&p);
  std::cout << *result << std::endl;
}
  • std::atomic_compare_exchange_strong_explicit[color ff0000]
  • std::move[link /reference/utility/move.md]
  • std::atomic_load[link atomic_load.md]

出力

3

バージョン

言語

  • C++11

処理系

参照