5

So from my understanding, mutex and binary semaphore are very similar but I just want to know what are some specific application or circumstances that using mutex is better than binary semaphore or viceversa

2 Answers 2

3

Generally speaking—since you did not mention any particular library or programming language—mutex and binary semaphore are very close to the same thing.

Binary semaphore is a specialization of the more general counting semaphore, which was invented way back in the early 1960s. It is a surprisingly versatile thing (see The Little Book of Semaphores, and back in the day, it was imagined that semaphore would be the lowest-level API, that would be built-in to many different operating systems to provide the bedrock upon which other, portable synchronization methods and algorithms could be built.

In my personal opinion, if you use something called "mutex" or "lock," then you should use it for one thing only: Use it to prevent threads from interfering with each other when they access shared variables. Whenever you think you want to use a mutex to let one thread send some kind of a signal to some other thread, then that's when you should reach for "semaphore." Even though they both do practically the same thing, using the one with the right name will help other people who read your code to understand what you are doing.

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

Comments

2

One big difference between a mutex and a binary semaphore is that a thread must not unlock a mutex locked by another thread (the thread locking the mutex is the unique ownership): a mutex is only meant to be used for critical sections. Wait conditions should be used in this case. A semaphore could be used to do that though it is a bit unusual. There are some other points about priority inversion and safety you can find here.

2 Comments

There's no reason, in principle, why a "mutex" locked in one scope cannot be unlocked in a different scope or, locked by one thread and unlocked by a different thread. Those "features" are arbitrary—added by the designers of some operating systems, some languages, and some libraries to help programmers to write code that is easier for others to understand and analyze. "Mutex" is just a name for an idea—an idea that can be realized by a "semaphore" object—and real objects called "semaphore" typically can be locked by one thread and unlocked by another.
Note: The OP only recently added the c tag to their question which may mean that they are using the Posix thread library (pthreads). In pthreads, the effect of pthread_mutex_unlock(m) is undefined if the mutex, m, was locked by a different thread. The WinAPI function, ReleaseMutex(m), is more reliable: It is guaranteed to "fail" if the mutex, m, was locked by a different thread. OTOH, If the question had been about Python, Python documentation explicitly says that a threading.Lock object may be released() by any thread—not only by the thread that "owns" it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.