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

履歴 編集

class
<mutex>

std::once_flag(C++11)

namespace std {
  struct once_flag;
}

概要

once_flagは、一度だけ指定された処理を呼び出すcall_once()関数で、呼び出し済みかどうかの状態フラグとして使用するクラスである。

メンバ関数

名前 説明 対応バージョン
(constructor) コンストラクタ C++11
~once_flag() = default デストラクタ C++11
operator=(const once_flag&) = delete 代入演算子 C++11

#include <iostream>
#include <thread>
#include <mutex>

std::once_flag once;

void init()
{
  // 初期化を行う...
  std::cout << "initialize" << std::endl;
}

void thread_proc()
{
  std::call_once(once, init);
}

int main()
{
  std::thread t1(thread_proc);
  std::thread t2(thread_proc);
  std::thread t3(thread_proc);

  t1.join();
  t2.join();
  t3.join();
}

出力

initialize

バージョン

言語

  • C++11

処理系

参照