Tuesday, October 14, 2014

What is the difference between a Mutex and a Semaphore?

Strictly speaking, mutex is a locking mechanism whereas semaphore is a signaling mechanism.
Mutex is the lock to a toilet and semaphore is the number of identical keys to a shared resource.
Imagine a buffer being split up into 4 parts and then the semaphore count can become 4, i.e there are now 4 instance to the same resource. Mind it , it is not the same resource. A semaphore with a value of 0 is similar to mutex but is has a difference, in mutex ,the thread within the same process can unlock the mutex whereas in semaphore an external thread can also free the semaphore by giving wakeup signal.

3 comments: