Is there any difference between a binary semaphore and mutex or are they essentially the same?
-
19They're semantically the same, but in practice you will notice weird differences (especially on Windows).Michael F– Michael F2009-10-09 05:30:17 +00:00Commented Oct 9, 2009 at 5:30
-
10@Michael Foukarakis: What are the weird differences?Philipp– Philipp2010-07-11 13:56:54 +00:00Commented Jul 11, 2010 at 13:56
-
5I suppose weird wasn't the correct expression. A mutex also supports ownership and sometimes reentry. This is the case in Windows. In addition, semaphores in Windows are implemented on top of Event objects, however, I'm unsure of the practical implications of this.Michael F– Michael F2010-07-11 15:13:44 +00:00Commented Jul 11, 2010 at 15:13
-
4@philipxy Nicely hid 'rn' in place of 'm'.Mooncrater– Mooncrater2018-10-20 11:09:41 +00:00Commented Oct 20, 2018 at 11:09
-
4@Mooncrater Wow. Good eye. I expect its due to OCR. (Yes, it is.)philipxy– philipxy2018-10-20 17:05:15 +00:00Commented Oct 20, 2018 at 17:05
37 Answers
The basic issue is concurrency. There is more than one flow of control. Think about two processes using a shared memory. Now only one process can access the shared memory at a time. If more than one process accesses the shared memory at a time, the contents of shared memory would get corrupted. It is like a railroad track. Only one train can run on it, else there would be an accident.So there is a signalling mechanism, which a driver checks. If the signal is green, the train can go and if it is red it has to wait to use the track. Similarly in case of shared memory, there is a binary semaphore. If the semaphore is 1, a process acquires it (makes it 0) and goes ahead and accesses it. If the semaphore is 0, the process waits. The functionality the binary semaphore has to provide is mutual exclusion (or mutex, in short) so that only one of the many concurrent entities (process or thread) mutually excludes others. It is a plus that we have counting semaphores, which help in synchronizing multiple instances of a resource.
Mutual exclusion is the basic functionality provided by semaphores. Now in the context of threads, we might have a different name and syntax for it. But the underlying concept is the same: how to keep integrity of code and data in concurrent programming. In my opinion, things like ownership, and associated checks are refinements provided by implementations.
Comments
- Mutex uses a locking mechanism i.e. if a process wants to use a resource then it locks the resource, uses it and then release it. But on the other hand, semaphore uses a signalling mechanism where wait() and signal() methods are used to show if a process is releasing a resource or taking a resource.
- A mutex is an object but semaphore is an integer variable.
- In semaphore, we have wait() and signal() functions. But in mutex, there is no such function.
- A mutex object allows multiple process threads to access a single shared resource but only one at a time. On the other hand, semaphore allows multiple process threads to access the finite instance of the resource until available.
- In mutex, the lock can be acquired and released by the same process at a time. But the value of the semaphore variable can be modified by any process that needs some resource but only one process can change the value at a time.
A useful read, I learned and copied from here
Comments
While there are over 30 answers, no one mentioned the following
In linux, there is another HUGE difference that pthread_mutex_lock() is not a cancellation point while sem_wait() is.
It is relevant when thread A hold a mutex/semaphore and want to pthread_cancel() thread B that is currently waiting that mutex/semaphore. If mutex is used, the behavior observed in my environment is that both thread A and thread B will wait each other and will be blocked forever
From man pthread_cancel
A thread's cancellation type, determined by pthread_setcanceltype(3), may be either asynchronous or deferred (the default for new threads). Deferred cancelability means that cancellation will be delayed until the thread next calls a function that is a can‐ cellation point. A list of functions that are or may be cancellation points is provided in pthreads(7).
And from man 7 pthreads
The following functions are required to be cancellation points by POSIX.1-2001 and/or POSIX.1-2008: ... sem_wait() ...
Suprisingly (at least to me), pthread_mutex_lock() is not on the list.
Please refer to Manual page pthreads(7) and pthread_cancel(3)
Comments
You can clearly remember difference by this:
Mutex lock : is for protecting critical region, Mutex can't be used across processes, only used in single process
Semaphore: is for signalling availability of a resource. Semaphore can be used both across processes and across processes.
1 Comment
Almost all of the above said it right. Let me also try my bit to clarify if somebody still has a doubt.
- Mutex -> used for serialization
- Semaphore-> synchronization.
Purpose of both are different however, same functionality could be achieved through both of them with careful programming.
Standard Example-> producer consumer problem.
initial value of SemaVar=0
Producer Consumer
--- SemaWait()->decrement SemaVar
produce data
---
SemaSignal SemaVar or SemaVar++ --->consumer unblocks as SemVar is 1 now.
Hope I could clarify.
2 Comments
stdout, or something like that, there's no obvious way to implement that with a mutex. Are you going to take / release a lock around every use of stdout? That doesn't even work, you wouldn't know whether the other thread has taken/released the mutex yet.