Skip to content

Latest commit

 

History

History
86 lines (62 loc) · 1.95 KB

File metadata and controls

86 lines (62 loc) · 1.95 KB

count_down

  • latch[meta header]
  • std[meta namespace]
  • latch[meta class]
  • function[meta id-type]
  • cpp20[meta cpp]
void count_down(ptrdiff_t update = 1);
  • ptrdiff_t[link /reference/cstddef/ptrdiff_t.md]

概要

ラッチのカウンタ値をupdateだけ減算し、カウンタ値が0になれば待機中スレッドのブロック解除を行う。

説明のため、ここではカウンタ値をcounterと表記する。

事前条件

update >= 0 かつ update <= counter

効果

アトミックにcounter -= updateを実行する。 counter0と等しければ、*this上で待機中の全スレッドをブロック解除する。

戻り値

なし

例外

この関数は、以下のerror conditionを持つsystem_error例外オブジェクトを送出する可能性がある:

#include <iostream>
#include <latch>
#include <thread>

int main()
{
  int shared_data = 0;
  std::latch stored{1};

  std::thread t([&]{
    // 通知を待機し、共有データから読取り
    stored.wait();
    std::cout << shared_data << std::endl;
  });

  // 共有データへ書込み、通知を行う
  shared_data = 42;
  stored.count_down();

  t.join();
}
  • count_down()[color ff0000]
  • wait()[link wait.md]

出力

42

バージョン

言語

  • C++20

処理系

関連項目