- semaphore[meta header]
- std[meta namespace]
- counting_semaphore[meta class]
- function[meta id-type]
- cpp20[meta cpp]
void release(ptrdiff_t update = 1);- ptrdiff_t[link /reference/cstddef/ptrdiff_t.md]
カウンティングセマフォのカウンタ値にupdateを加算し、待機中スレッドのブロック解除を行う。
説明のため、ここではカウンタ値をcounterと表記する。
update >= 0 かつ update <= max() - counter
アトミックにcounter += updateを実行し、counterが値0より大きくなるまで待機中のスレッド群をブロック解除する。
なし
この関数は、以下のerror conditionを持つsystem_error例外オブジェクトを送出する可能性がある:
resource_unavailable_try_again: 操作対象のネイティブハンドル型が無効operation_not_permitted: スレッドにこの操作を行う権限がないinvalid_argument: 実引数が無効
#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();
}- release()[color ff0000]
- acquire()[link acquire.md]
42
- C++20
- Clang: 11.0 [mark verified]
- GCC: ??
- ICC: ??
- Visual C++: ??