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

履歴 編集

function
<atomic>

std::atomic::operator++(C++11)

T operator++() volatile noexcept;             // (1) C++11
value_type operator++() volatile noexcept;    // (1) C++20

T operator++() noexcept;                      // (2) C++11
value_type operator++() noexcept;             // (2) C++20

T operator++(int) volatile noexcept;          // (3) C++11
value_type operator++(int) volatile noexcept; // (3) C++20

T operator++(int) noexcept;                   // (4) C++11
value_type operator++(int) noexcept;          // (4) C++20

概要

値をインクリメントする。

  • (1) : volatileオブジェクトに対する前置インクリメント
  • (2) : 非volatileオブジェクトに対する前置インクリメント
  • (3) : volatileオブジェクトに対する後置インクリメント
  • (4) : 非volatileオブジェクトに対する後置インクリメント

テンプレートパラメータ制約

  • (1), (3) :
    • C++20 : atomic<T>::is_always_lock_freetrueであること

戻り値

以下と等価:

例外

投げない

備考

この関数は、atomicクラスの整数型およびポインタに対する特殊化で定義される。

基本的な使い方

#include <iostream>
#include <atomic>

int main()
{
  std::atomic<int> x(3);

  ++x;

  std::cout << x.load() << std::endl;
}

出力

4

複数スレッドからインクリメントする例

#include <iostream>
#include <atomic>
#include <thread>

int main()
{
  std::atomic<int> x{0};

  // 複数スレッドでインクリメントを呼んでも、
  // 最終的に全てのスレッドでのインクリメントが処理された値になる
  std::thread t1 {[&x] {
    ++x;
  }};
  std::thread t2 {[&x] {
    ++x;
  }};

  t1.join();
  t2.join();

  std::cout << x.load() << std::endl;
}

出力

2

バージョン

言語

  • C++11

処理系

関連項目

参照