Difference between mutex and a semaphore

What is the difference between a mutex and a semaphore?

Answer:

According to Operating Systems by William Stallings (2008)*
-

Mutex is a binary semaphore (A semaphore that takes on only the values 0 and 1). The Key difference between the two is that the process that locks the mutex (sets the value to zero) must be one to unlock it (sets the value to 1).

According to "Professional C# 2005 with .NET 3.0
By Christian Nagel, Bill Evjen, Jay Glynn, Karli Watson, Morgan Skinner" -

A Semaphore can be used by mutliple threads (depending on the count) at once.

In brief,*
- By semaphore we actually mean counting semaphore. By mutex we actually mean mutex semaphore. A mutex is essentially a binary semaphore. You can replace any Mutex with a Semaphore of MaxCount 1.


- Mutexes are usually more efficient than binary semaphores.


- Mutex has an owner concept: unlocking a mutex can be only done by the thread that locked (or, equivalently, "owns") the mutex.


No comments: