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

履歴 編集

function
<atomic>

std::atomic_flag_test_and_set_explicit(C++11)

namespace std {
  bool atomic_flag_test_and_set_explicit(volatile atomic_flag* object, memory_order order) noexcept;
  bool atomic_flag_test_and_set_explicit(atomic_flag* object, memory_order order) noexcept;
}

概要

アトミックにテストしてフラグを立てる

効果

orderで指定されたメモリオーダーにしたがって、アトミックにtrue値を書き込む。この操作はread-modify-write操作である。

戻り値

変更前の値

例外

投げない

#include <iostream>
#include <atomic>

int main()
{
  std::atomic_flag x = ATOMIC_FLAG_INIT;

  std::cout << std::boolalpha;
  {
    // 値をtrueに設定する(変更前の値はfalse)
    bool result = std::atomic_flag_test_and_set_explicit(&x, std::memory_order_acq_rel);
    std::cout << result << std::endl;
  }
  {
    // 値をtrueに設定する(変更前の値はtrue)
    bool result = std::atomic_flag_test_and_set_explicit(&x, std::memory_order_acq_rel);
    std::cout << result << std::endl;
  }
}

出力

false
true

バージョン

言語

  • C++11

処理系

参照