Semaphore

We discussed semaphores in passing in Chapter 5,  Locks – Mutex, Condvar, Barriers, and RWLockespecially 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 ...

Get Hands-On Concurrency with Rust now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.