Difference between binary semaphore and mutex

2019-01-02 21:11发布

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

29条回答
至少爱过!
2楼-- · 2019-01-02 21:51

Since none of the above answer clears the confusion, here is one which cleared my confusion.

Strictly speaking, a mutex is a locking mechanism used to synchronize access to a resource. Only one task (can be a thread or process based on OS abstraction) can acquire the mutex. It means there will be ownership associated with mutex, and only the owner can release the lock (mutex).

Semaphore is signaling mechanism (“I am done, you can carry on” kind of signal). For example, if you are listening songs (assume it as one task) on your mobile and at the same time your friend called you, an interrupt will be triggered upon which an interrupt service routine (ISR) will signal the call processing task to wakeup.

Source: http://www.geeksforgeeks.org/mutex-vs-semaphore/

查看更多
忠厚老实头发少
3楼-- · 2019-01-02 21:52

In windows the difference is as below. MUTEX: process which successfully executes wait has to execute a signal and vice versa. BINARY SEMAPHORES: Different processes can execute wait or signal operation on a semaphore.

查看更多
beautiful°
4楼-- · 2019-01-02 21:53

Mutex: Suppose we have critical section thread T1 wants to access it then it follows below steps. T1:

  1. Lock
  2. Use Critical Section
  3. Unlock

Binary semaphore: It works based on signaling wait and signal. wait(s) decrease "s" value by one usually "s" value is initialize with value "1", signal(s) increases "s" value by one. if "s" value is 1 means no one is using critical section, when value is 0 means critical section is in use. suppose thread T2 is using critical section then it follows below steps. T2 :

  1. wait(s)//initially s value is one after calling wait it's value decreased by one i.e 0
  2. Use critical section
  3. signal(s) // now s value is increased and it become 1

Main difference between Mutex and Binary semaphore is in Mutext if thread lock the critical section then it has to unlock critical section no other thread can unlock it, but in case of Binary semaphore if one thread locks critical section using wait(s) function then value of s become "0" and no one can access it until value of "s" become 1 but suppose some other thread calls signal(s) then value of "s" become 1 and it allows other function to use critical section. hence in Binary semaphore thread doesn't have ownership.

查看更多
可以哭但决不认输i
5楼-- · 2019-01-02 21:56

Nice articles on the topic:

From part 2:

The mutex is similar to the principles of the binary semaphore with one significant difference: the principle of ownership. Ownership is the simple concept that when a task locks (acquires) a mutex only it can unlock (release) it. If a task tries to unlock a mutex it hasn’t locked (thus doesn’t own) then an error condition is encountered and, most importantly, the mutex is not unlocked. If the mutual exclusion object doesn't have ownership then, irrelevant of what it is called, it is not a mutex.

查看更多
▲ chillily
6楼-- · 2019-01-02 21:56

Myth:

Couple of article says that "binary semaphore and mutex are same" or "Semaphore with value 1 is mutex" but the basic difference is Mutex can be released only by thread that had acquired it, while you can signal semaphore from any other thread

Key Points:

•A thread can acquire more than one lock (Mutex).

•A mutex can be locked more than once only if its a recursive mutex, here lock and unlock for mutex should be same

•If a thread which had already locked a mutex, tries to lock the mutex again, it will enter into the waiting list of that mutex, which results in deadlock.

•Binary semaphore and mutex are similar but not same.

•Mutex is costly operation due to protection protocols associated with it.

•Main aim of mutex is achieve atomic access or lock on resource

查看更多
成全新的幸福
7楼-- · 2019-01-02 21:58

The answer may depend on the target OS. For example, at least one RTOS implementation I'm familiar with will allow multiple sequential "get" operations against a single OS mutex, so long as they're all from within the same thread context. The multiple gets must be replaced by an equal number of puts before another thread will be allowed to get the mutex. This differs from binary semaphores, for which only a single get is allowed at a time, regardless of thread contexts.

The idea behind this type of mutex is that you protect an object by only allowing a single context to modify the data at a time. Even if the thread gets the mutex and then calls a function that further modifies the object (and gets/puts the protector mutex around its own operations), the operations should still be safe because they're all happening under a single thread.

{
    mutexGet();  // Other threads can no longer get the mutex.

    // Make changes to the protected object.
    // ...

    objectModify();  // Also gets/puts the mutex.  Only allowed from this thread context.

    // Make more changes to the protected object.
    // ...

    mutexPut();  // Finally allows other threads to get the mutex.
}

Of course, when using this feature, you must be certain that all accesses within a single thread really are safe!

I'm not sure how common this approach is, or whether it applies outside of the systems with which I'm familiar. For an example of this kind of mutex, see the ThreadX RTOS.

查看更多
登录 后发表回答