Appearance
16. Binary Semaphore vs Mutex
They look identical (both restrict to one thread) but differ fundamentally:
- Ownership — enforced in a mutex, absent in a binary semaphore.
- Purpose — mutex = mutual exclusion; binary semaphore = signaling.
- Priority inversion — mutexes typically support priority inheritance; semaphores don't (no ownership to boost).
- Recursive locking — a recursive mutex can be relocked by its owner; a binary semaphore deadlocks if the same thread calls
wait()twice. - 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.