Article

Understanding std::counting_semaphore and std::binary_semaphore from C++20

C++ Stories ·C++ Stories ·Published 2026-08-08 ·5 min read

This 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


Discovered 2026-08-10 Source C++ Stories Archive 2026-08 → c++c++20multithreadingsemaphores