We discussed semaphores in passing in Chapter 5, especially with regard to the concurrency puzzles from The Little Semaphore. It's now, as promised, time to implement a semaphore. What exactly is a semaphore? Similar to our analysis of mutex as an atomic object, let's consider it:
- Semaphore supports two operations, wait and signal.
- A semaphore has an isize value that is used to track the available resource capacity of the semaphore. This value is only manipulable by wait and signal.
- The operation wait decrements value. If the value is less than zero, 'wait' blocks the calling thread until such time as a value becomes available. If the value is not less than zero, the thread does not ...