A semaphore as a mutex

We just asked the question "Could you do it with a mutex?" in relation to implementing a lock with a count, and the answer was no. How about the other way around? Could we use a semaphore as a mutex?

Yes. In fact, in some operating systems, that's exactly what they do—they don't have mutexes, only semaphores! So why bother with mutexes at all?

To answer that question, look at your washroom. How did the builder of your house implement the "mutex"? I suspect you don't have a key hanging on the wall!

Mutexes are a "special purpose" semaphore. If you want one thread running in a particular section of code, a mutex is by far the most efficient implementation.

Later on, we'll look at other synchronization schemes—things called condvars, barriers, and sleepons.

Note: Just so there's no confusion, realize that a mutex has other properties, such as priority inheritance, that differentiate it from a semaphore.