Understanding std::counting_semaphore and std::binary_semaphore from C++20
cppdashboard.dev/r/2026/08/understanding-std-counting-semaphore-and-std-binary-semaphorThis article explains the two semaphore types introduced in C++20: std::counting_semaphore and std::binary_semaphore. It covers their usage in multi-threading scenarios and provides examples.
From the article
This article explains the two semaphore types introduced in C++20: std::counting_semaphore and std::binary_semaphore . We’ll first use a counting semaphore to limit how many threads can operate at the same time. Then we’ll use a binary semaphore to send a signal between threads. We’ll also look at timed waiting, a small RAII helper, and a few more details.
Note: The synchronization features discussed here are available in C++20. The examples use C++23 std::println for cleaner output.
A mutex works well when only one thread should enter a protected section at a time. But sometimes that limit is too strict.
Share this resource