• Class / Function / Type

      std::
    • Header file

      <>
    • Other / All

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

    履歴 編集

    function
    <semaphore>

    std::counting_semaphore::release

    void release(ptrdiff_t update = 1);
    

    概要

    カウンティングセマフォのカウンタ値にupdateを加算し、待機中スレッドのブロック解除を行う。

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

    事前条件

    update >= 0 かつ update <= max() - counter

    効果

    アトミックにcounter += updateを実行し、counterが値0より大きくなるまで待機中のスレッド群をブロック解除する。

    戻り値

    なし

    例外

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

    #include <iostream>
    #include <semaphore>
    #include <thread>
    
    int main()
    {
      int shared_data = 0;
      std::counting_semaphore sem{0};
    
      std::thread t([&]{
        // 通知を待機し、共有データから読取り
        sem.acquire();
        std::cout << shared_data << std::endl;
      });
    
      // 共有データへ書込み、通知を行う
      shared_data = 42;
      sem.release();
    
      t.join();
    }
    

    出力

    42
    

    バージョン

    言語

    • C++20

    処理系