Is there any difference between a Binary Semaphore and Mutex?

Technology CommunityCategory: ConcurrencyIs there any difference between a Binary Semaphore and Mutex?
VietMX Staff asked 3 years ago
  • mutex (or Mutual Exclusion Semaphores) is a locking mechanism used to synchronize access to a resource. Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock (mutex).
  • Semaphore (or Binary Semaphore) is signaling mechanism (“I am done, you can carry on” kind of signal). For example, if you are listening songs (assume it as one task) on your mobile and at the same time your friend called you, an interrupt will be triggered upon which an interrupt service routine (ISR) will signal the call processing task to wakeup. A binary semaphore is NOT protecting a resource from access. Semaphores are more suitable for some synchronization problems like producer-consumer.

Short version:

  • mutex can be released only by the thread that had acquired it.
  • binary semaphore can be signaled by any thread (or process).