Coder Perfect

Mutex vs. semaphore in pthreads

Problem

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 frequently used to determine how many pieces of a resource are in use — for example, an object representing n worker threads might use a semaphore to determine how many worker threads are accessible.

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, allowing the thread that is currently waiting (blocked) in the mutex lock to unblock and access the data to do so? (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

Mutex:

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 serialise 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 which attempt to gain access to that section to wait until the first thread has exited from that section.”

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

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).”

Source

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.

mutex can’t be replaced with binary semaphore since, one process waits for semaphore while other process releases semaphore. In case mutex both acquisition and release is handled by same.

Answered by C Learner

Solution #5

The distinction between a semaphore and a mutex is one of mechanism versus pattern. The distinction is in their goals (intent) and how they operate (behavioral).

The mutex, barrier, pipeline are parallel programming patterns. 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 utilized 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.

Summary,

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 https://stackoverflow.com/questions/2065747/pthreads-mutex-vs-semaphore