Putereads mutex vs semaphore

What is the difference between semaphores and mutex provided by pthread library?

+61
c synchronization linux pthreads mutex
Jan 14 '10 at
source share
8 answers

semaphores have a synchronized counter, and a mutex has only a binary (true / false).

A semaphore is often used as the ultimate mechanism for answering how many resource elements are used — for example, an object that represents n workflows can use a semaphore to count the number of workflows.

The truth is that you can represent an INT semaphore that is synchronized using a mutex.

+66
Jan 14 '10 at 16:38
source share

I am going to talk about Mutex vs Binary-Semaphore. You obviously use a mutex to prevent data from being accessed simultaneously in one thread by another thread.

(Suppose you just called lock () while accessing data. This means that you do not expect any other thread (or another instance of the same thread) to access the same data, one and the same mutex. That is, if it is the same thread code executed on a different thread instance, it locks the lock, then lock () should block the control flow.)

This refers to a thread that uses a different code stream, which also accesses the same data and which is also blocked by the same mutec.

In this case, you are still in the process of accessing the data, and you can take, say, another 15 seconds to achieve unlocking the mutex (so another thread that locks in the mutex lock will unlock and allow the control to access the data) .

Have you ever allowed another thread to simply unlock the same mutex and, in turn, allow a thread that already expects (blocking) the mutex lock to unlock and access the data? (Hope you got what I say here.)

According to an agreed universal definition

  • with a mutex, this cannot happen. No other thread can unlock a lock in your thread
  • with a "binary semaphore" this can happen. Any other thread can unlock a lock in your thread.

So, if you especially relate to using a binary semaphore instead of a mutex, then you should be very careful in "defining areas" of locks and unlocks, I mean that every control flow that falls into each lock should hit the unlock and not should be the "first unlock", rather, it will always be the "first lock".

+16
Dec 11 '12 at 17:30
source share

mutex is used to avoid race conditions between multiple threads.

whereas a semaphore is used as a synchronization element used for several processes.

mutex cannot be replaced with a binary semaphore, as one process expects a semaphore and another process frees the semaphore. In the event that the mutex and the reception and release are processed the same.

+7
Jan 15
source share

These two articles explain great details about mutex vs semaphores. Also this response to stack overflow says a similar answer.

+2
Nov 04 '12 at 19:44
source share

The difference between semaphore and mutex is the difference between a mechanism and a template . The difference in their purpose ( intention ) and how they work ( behavioral ).

mutex , barrier , pipeline are parallel programming patterns . mutex used ( intended ) to protect the critical section and ensure mutual exclusion . barrier forces agents (thread / process) to continue to wait for each other.

One of the characteristics ( behavior ) of the mutex template is that only an authorized agent (processes or thread) can enter the critical section and only that agent can voluntarily get from this.

There are times when mutex allows a one-time agent. There are times when it allows several agents (multiple readers) and prohibit some other agents (writers).

semaphore is a mechanism that you can use ( intended ) to implement various patterns. This ( behavior ) is usually a flag (possibly protected by mutual exclusion). (One interesting fact - even the mutex template can be used to implement the semaphore).

In a popular culture, semaphores are mechanisms provided by the kernels, and mutexes provided by the user space library.

Note that there are misconceptions about semaphores and mutexes . It says that semaphores are used for synchronization . And mutexes has ownership . This is due to popular OS books. But truth is all mutexes, semaphores, and barriers used for synchronization . The purpose of the mutex is not ownership , but mutual exclusion . This fallacy led to a popular interview asking about the difference mutexes and binary-semaphores .

Summary

intention
  • mutex, mutual exclusion
  • semaphore, implement parallel design patterns
behavior
  • mutex, only the authorized agent enters the critical section and only he (they) can exit
  • semaphore, enter if the flag says "go", otherwise wait until someone changes the flag

In a project perspective, mutex more like a state-pattern , where an algorithm selected by the state can change state. binary-semaphore more like a strategy-pattern , where an external algorithm can change state and, ultimately, the algorithm / strategy chosen to run.

+2
May 17 '16 at 17:30
source share

The semaphore is more used as a flag for which you really don't need to bring RTOS / OS. A semaphore can be accidentally or intentionally modified by other threads (say, due to poor coding). When you use a mutex stream, it owns resources. No other thread can access it before the resource becomes free.

+1
Sep 01 '15 at 2:17
source share

Toilet example

mutex:

Is the key to the toilet. One person may have a key - take a toilet - at a time. Upon completion, the person gives (releases) the key to the next person in the queue.

"Mutexes are typically used to serialize access to a section of reentry code that cannot be executed at the same time by more than one thread. The mutex object allows only one thread to a controlled section, causing other threads to try to access this section to wait until the first thread has will exit this section. "

(Mutex is indeed a semaphore with a value of 1.)

Semaphore:

Is the amount of free identical toilet keys. For example, let's say we have four toilets with the same locks and keys. The semaphore count — the number of keys — is set at the beginning of 4 (all four toilets are free), then the count decreases as people arrive. If all the toilets are full, that is. there are no free keys, the semaphore count is 0. Now, when the equation one person leaves the toilet, the semaphore increases to 1 (one free key) and is transmitted to the next person in the queue.

"A semaphore limits the number of simultaneous users of a shared resource to a maximum number. Topics can request access to a resource (decreasing the semaphore) and may signal that they have finished using the resource (increasing the semaphore)."

Source

+1
Jul 10 '16 at 19:33
source share

Mutexes can only be applied to threads in one process and do not work between processes, like semaphores.

0
May 08 '11 at
source share



All Articles