Difference between Mutex and Semaphore

Besides the better readability, the code for mutex is smaller and slightly faster than the semaphore implementation. Further, since a mutex can only be acquired by a single entity, a lot of debugging features could be added, which is otherwise not possible with the generic semaphores. And finally, the mutex semantics is less error prone because of stricter semantics.

  • only one task can hold the mutex at a time
  • only the owner can unlock the mutex
  • multiple unlocks are not permitted
  • recursive locking is not permitted
  • a mutex object must be initialized via the API
  • a mutex object must not be initialized via memset or copying
  • task may not exit with mutex held
  • memory areas where held locks reside must not be freed
  • held mutexes must not be reinitialised
  • mutexes may not be used in hardware or software interrupt contexts such as tasklets and timers

Reference – https://lwn.net/Articles/164802/

Leave a Comment