Namespaces
Variants

std:: counting_semaphore, std:: binary_semaphore

From cppreference.net
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
Generic lock management
Condition variables
(C++11)
Semaphores
counting_semaphore binary_semaphore
(C++20) (C++20)
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
Safe reclamation
Hazard pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11) (deprecated in C++20)
(C++11) (deprecated in C++20)
Memory ordering
(C++11) (deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
ヘッダーで定義 <semaphore>
template < std:: ptrdiff_t LeastMaxValue = /* implementation-defined */ >
class counting_semaphore ;
(1) (C++20以降)
using binary_semaphore = std :: counting_semaphore < 1 > ;
(2) (C++20以降)
1) A counting_semaphore は、共有リソースへのアクセスを制御できる軽量な同期プリミティブです。 std::mutex とは異なり、 counting_semaphore は同じリソースへの複数の同時アクセスを許可し、少なくとも LeastMaxValue 個の同時アクセッサをサポートします。 LeastMaxValue が負の値の場合、プログラムは不適格となります。
2) binary_semaphore は、 LeastMaxValue 1 である std::counting_semaphore の特殊化のエイリアスです。実装によっては、 binary_semaphore std::counting_semaphore のデフォルト実装よりも効率的に実装する場合があります。

counting_semaphore は、コンストラクタで初期化される内部カウンタを持ちます。このカウンタは acquire() および関連メソッドの呼び出しによって減少し、 release() の呼び出しによって増加します。カウンタがゼロの場合、 acquire() はカウンタが増加するまでブロックしますが、 try_acquire() はブロックしません。 try_acquire_for() try_acquire_until() は、カウンタが増加するかタイムアウトに達するまでブロックします。

std::condition_variable::wait() と同様に、 counting_semaphore try_acquire() は偽の失敗を起こす可能性があります。

std::counting_semaphore の特殊化は、 DefaultConstructible CopyConstructible MoveConstructible CopyAssignable 、または MoveAssignable ではありません。

目次

データメンバ

メンバー名 定義
counter (private) std::ptrdiff_t 型の内部カウンター。
( 説明専用メンバーオブジェクト* )

メンバー関数

counting_semaphore を構築する
(public member function)
counting_semaphore を破棄する
(public member function)
operator=
[deleted]
counting_semaphore は代入不可
(public member function)
操作
内部カウンタをインクリメントし、取得側のブロックを解除する
(public member function)
内部カウンタをデクリメントするか、可能になるまでブロックする
(public member function)
ブロックせずに内部カウンタのデクリメントを試みる
(public member function)
内部カウンタのデクリメントを試み、最大で指定時間ブロックする
(public member function)
内部カウンタのデクリメントを試み、指定時点までブロックする
(public member function)
定数
[static]
内部カウンタの最大可能値を返す
(public static member function)

注記

その名が示す通り、 LeastMaxValue 最小 の最大値であり、 実際の 最大値ではありません。したがって max() LeastMaxValue よりも大きな値を返す可能性があります。

std::mutex とは異なり、 counting_semaphore は実行スレッドに紐付けられていません - 例えば、セマフォの獲得はセマフォの解放とは異なるスレッドで発生することができます。 counting_semaphore に対するすべての操作は、特定の実行スレッドとの関係なく並行して実行できますが、デストラクタは例外で、並行して実行することはできませんが、異なるスレッドで実行することは可能です。

<p
機能テスト マクロ 標準 機能
__cpp_lib_semaphore 201907L (C++20) std::counting_semaphore , std::binary_semaphore

#include <chrono>
#include <iostream>
#include <semaphore>
#include <thread>
// グローバルなバイナリセマフォインスタンス
// オブジェクトカウントは0に設定
// オブジェクトは非シグナル状態
std::binary_semaphore
    smphSignalMainToThread{0},
    smphSignalThreadToMain{0};
void ThreadProc()
{
    // メインプロセスからのシグナルを待機
    // セマフォのデクリメントを試行することで
    smphSignalMainToThread.acquire();
    // この呼び出しは、メインプロセスからセマフォのカウントが
    // 増加されるまでブロックする
    std::cout << "[thread] Got the signal\n"; // 応答メッセージ
    // スレッドによる作業を模倣するため3秒間待機
    // スレッドによる何らかの作業
    using namespace std::literals;
    std::this_thread::sleep_for(3s);
    std::cout << "[thread] Send the signal\n"; // メッセージ
    // メインプロセスにシグナルを返す
    smphSignalThreadToMain.release();
}
int main()
{
    // ワーカースレッドを作成
    std::thread thrWorker(ThreadProc);
    std::cout << "[main] Send the signal\n"; // メッセージ
    // ワーカースレッドに作業開始をシグナル
    // セマフォのカウントを増加させることで
    smphSignalMainToThread.release();
    // ワーカースレッドの作業完了を待機
    // セマフォのカウントをデクリメントしようとすることで
    smphSignalThreadToMain.acquire();
    std::cout << "[main] Got the signal\n"; // 応答メッセージ
    thrWorker.join();
}

出力:

[main] Send the signal
[thread] Got the signal
[thread] Send the signal
[main] Got the signal