![]() women's), which is analogous to using two distinct mutexes. ![]() 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. 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. The visitor only knows that he has a key, not yet which bathroom is free. 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).Ī semaphore cannot solve a multiple identical resource problem on its own. This simple resource protection protocol does not scale to the case of two equivalent bathrooms. 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. If you ask to use the bathroom when the key is not available, you are asked to wait in a queue for the key. At the coffee shop, there is one bathroom and one bathroom key. If you think of a mutex as a key owned by the operating system, it is easy to see that an individual mutex is analogous to the bathroom key owned by an urban coffee shop. It is easiest to explain why the "multiple resource" scenario is flawed by way of an analogy. 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. 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. The most common (but nonetheless incorrect) answer to the question posed at the top is that mutexes and semaphores are very similar, with the only significant difference being that semaphores can count higher than one. Reality: While mutexes and semaphores have some similarities in their implementation, they should always be used differently. Myth: Mutexes and Semaphores are Interchangeable In this article, I aim to distinguish these two important RTOS primitives once and for all, by debunking a popular myth about their similarity. View our archived webinar on this and related topics. This is unfortunate and dangerous, as misuse of either RTOS primitive can easily lead to unintended errors in embedded systems that could turn them into life-threatening products. In this first installment of a series of articles on the proper use of a real-time operating system (RTOS), we examine the important differences between a mutex and a semaphore.Īfter conversations with countless embedded software developers over many years, I have concluded that even very experienced RTOS users have trouble distinguishing the proper uses of mutexes and semaphores. The question "What is the difference between a mutex and a semaphore?" is short and easily phrased.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |