Skip to content

16. Binary Semaphore vs Mutex

They look identical (both restrict to one thread) but differ fundamentally:

  1. Ownership — enforced in a mutex, absent in a binary semaphore.
  2. Purpose — mutex = mutual exclusion; binary semaphore = signaling.
  3. Priority inversion — mutexes typically support priority inheritance; semaphores don't (no ownership to boost).
  4. Recursive locking — a recursive mutex can be relocked by its owner; a binary semaphore deadlocks if the same thread calls wait() twice.
  5. Initialization — a mutex always starts unlocked; a binary semaphore can start at 0, so the first wait() blocks — useful for signaling patterns.

ISR example (semaphore-only use case): an interrupt handler needs to notify a thread that data is ready, but an ISR can't hold locks or block. sem_signal() from the ISR works fine; mutex_unlock() would be illegal since the ISR never locked it.