1020

Is there any difference between a binary semaphore and mutex or are they essentially the same?

10
  • 19
    They're semantically the same, but in practice you will notice weird differences (especially on Windows). Commented Oct 9, 2009 at 5:30
  • 10
    @Michael Foukarakis: What are the weird differences? Commented Jul 11, 2010 at 13:56
  • 5
    I 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. Commented Jul 11, 2010 at 15:13
  • 4
    @philipxy Nicely hid 'rn' in place of 'm'. Commented Oct 20, 2018 at 11:09
  • 4
    @Mooncrater Wow. Good eye. I expect its due to OCR. (Yes, it is.) Commented Oct 20, 2018 at 17:05

37 Answers 37

1
2
0

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.

Sign up to request clarification or add additional context in comments.

Comments

0

Mutex is a Criteria(Out of 4) that any algorithm that intends to solve the Critical Section Problem must follow whereas binary semaphore in itself is a variable that can take values 0 and 1.

Comments

0
  • 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

0

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

-2

"binary semaphore" is a programming language circumvent to use a «semaphore» like «mutex». Apparently there are two very big differences:

  1. The way you call each one of them.

  2. The maximum length of the "identifier".

Comments

-3

You can clearly remember difference by this:

  1. Mutex lock : is for protecting critical region, Mutex can't be used across processes, only used in single process

  2. Semaphore: is for signalling availability of a resource. Semaphore can be used both across processes and across processes.

1 Comment

Interprocess mutexes are possible: stackoverflow.com/questions/9389730/…
-4

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

same functionality could be achieved through both of them. A mutex might check that it's only unlocked by the same thread that locked it, because anything else is an error for a mutex. If you want to wait until another thread has redirected 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.
If you remove that claim, the example is maybe useful.
1
2

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.