Design Article
Don't believe everything you hear about RTOSes
Michael Barr
10/1/2008 12:41 PM EDT
The question "What's the difference between a mutex and a semaphore?" is short and easily phrased. Answering it is far more difficult. After conversations with countless embedded software developers over many years, I've concluded that even experienced RTOS users have trouble distinguishing the proper uses of mutexes and semaphores. This is unfortunate and dangerous, as misuse of either RTOS primitive can easily lead to unintended errors in embedded systems, including life-threatening products.
While the myth is that mutexes and semaphores are similar or even interchangeable, the truth is that while mutexes and semaphores have similarities in their implementation, they should always be used differently.
The most common (but nonetheless incorrect) answer is that mutexes and semaphores are very similar, with the only significant difference being that semaphores can count higher than one. Nearly all engineers seem to properly understand that a mutex is a binary flag used to protect a shared resource by ensuring mutual exclusion inside critical sections of code. But when asked to expand on how to use a "counting semaphore," most engineers-varying only in their degree of confidence-express some flavor of the textbook opinion that these are used to protect several equivalent resources.1
It's easiest to explain why the "multiple resource" scenario is flawed by way of an analogy. If you think of a mutex as a key owned by the OS, it's easy to see that an individual mutex is analogous to the bathroom key owned by an urban coffee shop. At the coffee shop, there's one bathroom and one bathroom key. If you ask to use the bathroom when the key is not available, you are asked to wait in a queue for the key. By a very similar protocol, a mutex helps multiple tasks serialize their accesses to shared global resources and gives waiting tasks a place to wait for their turn.
This simple resource-protection protocol doesn't scale to the case of two equivalent bathrooms. If a semaphore were a generalization of a mutex able to protect two or more identical shared resources, then in our analogy it would be a basket containing two identical keys (i.e., each of the keys would work in either bathroom door).
A semaphore can't solve a multiple identical resource problem on its own. The visitor only knows that he has a key, but not which bathroom is free. If you try to use a semaphore like this, you'll find you always need other state information-itself typically a shared resource protected via a separate mutex.2 It turns out the best way to design a two-bathroom coffee shop is to offer distinct keys to distinct bathrooms (e.g., men's vs. women's), which is analogous to using two distinct mutexes.



cpns
10/1/2008 10:19 PM EDT
"[...]and make matters worse by introducing the additional names "binary semaphore" (for mutex) and "counting semaphore."
Does that statement not also add to the confusion?
A binary semaphore is not a mutex either, since it lacks a priority inversion protocol, and can be taken and given by separate tasks. A binary semaphore is rather an intertask signalling or synchronisation method.
VxWorks is clear for example (although that is no endorsement!), it has three distinct semaphore types, counting, binary, and mutex.
Sign in to Reply
Theckyam
10/2/2008 8:56 AM EDT
This article should be an eye opener for many embedded engineers. Even though Semaphores are and can be widely used for signaling, there are separate APIs for Events or Signals in many of the RTOSes.
My understanding about the need for events is to have a optimized method for signaling to one or more tasks at the same time.
This may be similar to events that are provided by Windows for signaling to multiple tasks.
I believe that most RTOSes "SHOULD" have events more optimized for signaling between tasks than a semaphore, which serves more generic purposes; though, at this point I am not able to give conclusive evidence that events *are* more optimized than semaphores.
If someone can throw light on this too (Or Mr.Barr can speak from any of his experiences), I think it would also benefit the embedded community.
Saravanan T S
Sign in to Reply
deathwillarrive
10/3/2008 3:50 AM EDT
yeah ... vxworks provides three stiff
mutex, binary semaphore and counting semaphores. And Mutexes are binary semaphores with an added priority inversion protocol.
Also the OS maintained queues are internally implemented using mutexes.
It should be mentioned that we should prefer semaphores over mutexes if all the tasks which uses the semaphores are of same priority
Sign in to Reply
NSC
10/14/2008 5:52 AM EDT
There are a couple of issues here. The most important distinction between a semaphore and a mutex is the concept of ownership. Only the task/thread that locked the mutex can unlock it. Whereas semaphores (either binary or counting - they are different) anyone can "release" them. This allows the abuse of sempahores as signals (again totally different concepts). The other typical difference is recursive locking (not a good thing in itself) but most mutex allow the same task/thread to re-lock a mutex they own.
Sign in to Reply
freezykid
12/15/2008 4:02 AM EST
I had always had a feeling mutex is more than a binary semaphore. Explaining the usage of semaphore in priority inversion cases was good, though I need to check RMA. The usage difference of sem and mutex can be more easily understood from the examples.
Sign in to Reply