Coder Perfect

Mutex vs. semaphore in pthreads


What is the difference between pthread library’s semaphores and mutex?

Asked by cppdev

Solution #1

Mutexes are only binary (true / false) and semaphores have a synchronized counter.

A semaphore is often used as a definitive mechanism for answering how many elements of a resource are in use — e.g., an object that represents n worker threads might use a semaphore to count how many worker threads are available.

The truth is that a semaphore can be represented by an INT that is synchronized by a mutex.

Answered by Hassan Syed

Solution #2

I’m going to compare and contrast Mutex with Binary-Semaphore. You use mutex to prohibit data from being accessed by two threads at the same time.

(Assume you’ve just called lock() and are trying to access some data.) This indicates that no other thread (or instance of the same thread-code) should be able to access the data locked by the same mutex. If the identical thread-code is performed on a separate thread instance and it reaches the lock, the lock() function should halt control flow.)

This is true for a thread that runs on a different thread-code, accesses the same data, and is locked by the same mutex.

In this situation, you’re still reading the data, so it can take another 15 seconds to get to the mutex unlock (so that the other thread that is getting blocked in mutex lock would unblock and would allow the control to access the data).

Do you ever allow another thread to just unlock the same mutex, and in turn, allow the thread that is already waiting (blocking) in the mutex lock to unblock and access the data? (I hope you understand what I’m saying.)

According to a universally agreed-upon definition,

So, if you insist on utilizing a binary-semaphore rather than a mutex, you need be very careful about “scoping” the locks and unlocks, which means that any control-flow that hits a lock should also hit an unlock call, and there should never be a “first unlock,” but always “first lock.”

Answered by Paxi

Solution #3

The Toilet Example


Is a toilet’s key. At any given time, one individual can have the key and use the toilet. When the person is completed, he or she passes (releases) the key to the next person in line.

“Mutexes are typically used to serialize access to a section of re-entrant code that cannot be executed concurrently by more than one thread; a mutex object only allows one thread into a controlled section, forcing other threads attempting to gain access to that section to wait until the first thread has exited.”

(A mutex is actually a one-valued semaphore.)


Is the amount of identical toilet keys that are available for free. Let’s pretend we have four toilets with the same locks and keys. The semaphore count – the number of keys – is set to 4 at the start (all four toilets are free), then decremented when more people arrive. The semaphore count is 0 if all toilets are full, i.e. there are no free keys left. When eq. one person leaves the restroom, the semaphore is increased to 1 (one free key), and the next person in line is given the key.

“A semaphore limits the number of concurrent users of a shared resource to a certain limit. Threads can both request access to the resource (decrementing the semaphore) and signal when they are done utilizing it (incrementing the semaphore).”


Answered by Ankur

Solution #4

A mutex is used to prevent a race between multiple threads.

Semaphore, on the other hand, is a synchronizing element that is used across numerous processes.

Because one process waits for semaphore while the other releases it, mutex cannot be replaced with binary semaphore. When using a mutex, both acquisition and release are handled by the same object.

Answered by C Learner

Solution #5

The difference between the semaphore and mutex is the difference between mechanism and pattern. The difference is in their purpose (intent)and how they work(behavioral).

Parallel programming patterns include mutex, barrier, and pipeline. Mutex is a type of lock that is used (or intended to be used) to protect a critical region and assure mutual exclusion. The agents (threads/processes) are forced to wait for each other due to the barrier.

One of the characteristics (behavior) of the mutex pattern is that only authorized agents (processes or threads) can enter a critical area, and only those agents (processes or threads) can freely exit it.

In some circumstances, mutex only permits one agent to run at a time. There are times when it accepts many agents (readers) while disallowing others (writers).

The semaphore is a pattern-implementing tool that can be used(intended) in a variety of ways. It’s (behavior) usually a red flag (possibly protected by mutual exclusion). (An interesting point is that semaphore can be implemented using the mutex pattern.)

Kernels provide semaphores, whereas user-space libraries offer mutexes, according to popular belief.

There are certain misunderstandings about semaphores and mutexes. Semaphores are said to be used for synchronization. And mutexes is the owner. This is owing to the popularity of popular OS books. However, all mutexes, semaphores, and barriers are actually employed for synchronization. The goal of mutex is mutual exclusion rather than ownership. This misunderstanding led to a common interview question about the differences between mutexes and binary-semaphores.


Mutex is more like a state-pattern in terms of design, where the algorithm chosen by the state can change the state. The binary-semaphore is more akin to a strategy-pattern in which an external algorithm can change the state, causing the algorithm/strategy to run.

Answered by KRoy

Post is based on