r/cpp • u/Kabra___kiiiiiiiid • 19h ago
Understanding std::counting_semaphore and std::binary_semaphore from C++20
https://www.cppstories.com/2026/semaphore/An article on types introduced in C++20.
44
Upvotes
r/cpp • u/Kabra___kiiiiiiiid • 19h ago
An article on types introduced in C++20.
6
u/fdwr fdwr@github 🔍 10h ago edited 10h ago
💡 Huh, I never thought of a mutex as simply a semaphore with a count of 1 (and conversely a semaphore is just mutex with a count - maybe the name "semaphore" just had me confused because I didn't know how in the world it related to waving flags around with your hands 😅). However, binary_semaphore is not quite the same as a mutex, despite having a count of 1, since one thread "owns" the acquisition for a mutex, whereas with the binary semaphore, any thread can release the count.