A counting semaphore.
This is an integer that is always positive, and has two main operations: increment (post) and decrement (wait). If a decrement can not be performed (i.e. the value is 0) the caller will be blocked until another thread posts and the operation can succeed.
Semaphores can be created with any starting value, but typically this will be 0 so the semaphore can be used as a simple signal where each post corresponds to one wait.
Semaphores are very efficient (much more so than a mutex/cond pair). In particular, at least on Linux, post is async-signal-safe, which means it does not block and will not be interrupted. If you need to signal from a realtime thread, this is the most appropriate primitive to use.
- Note
- Likely outdated with C++20's std::counting_semaphore (though we have to check that this will be RT conforming on all platforms)