Is there any difference between a binary semaphore and mutex or are they essentially the same?
相关问题
- Why it isn't advised to call the release() met
- Why should we check WIFEXITED after wait in order
- Linux kernel behaviour on heap overrun or stack ov
- Is there a difference between an ISR and an interr
- (assembly x86 real mode) Data gets “cut off” at th
相关文章
- What happens to dynamic allocated memory when call
- Including std::lock_guard in extra scope
- java Semaphore north south que
- Delete an object securely from a multi-threaded pr
- How can the linux bottom halfs execute in interrup
- How is copy paste possible?
- How to generate directory size recursively in pyth
- run Java program with jar from Python
Though mutex & semaphores are used as synchronization primitives ,there is a big difference between them. In the case of mutex, only the thread that locked or acquired the mutex can unlock it. In the case of a semaphore, a thread waiting on a semaphore can be signaled by a different thread. Some operating system supports using mutex & semaphores between process. Typically usage is creating in shared memory.
On Windows, there are two differences between mutexes and binary semaphores:
A mutex can only be released by the thread which has ownership, i.e. the thread which previously called the Wait function, (or which took ownership when creating it). A semaphore can be released by any thread.
A thread can call a wait function repeatedly on a mutex without blocking. However, if you call a wait function twice on a binary semaphore without releasing the semaphore in between, the thread will block.
http://www.geeksforgeeks.org/archives/9102 discusses in details.
Mutex
is locking mechanism used to synchronize access to a resource.Semaphore
is signaling mechanism.Its up to to programmer if he/she wants to use binary semaphore in place of mutex.
Mutex and binary semaphore are both of the same usage, but in reality, they are different.
In case of mutex, only the thread which have locked it can unlock it. If any other thread comes to lock it, it will wait.
In case of semaphone, that's not the case. Semaphore is not tied up with a particular thread ID.
Their synchronization semantics are very different:
As such one can see a mutex as a token passed from task to tasks and a semaphore as traffic red-light (it signals someone that it can proceed).
They are NOT the same thing. They are used for different purposes!
While both types of semaphores have a full/empty state and use the same API, their usage is very different.
Mutual Exclusion Semaphores
Mutual Exclusion semaphores are used to protect shared resources (data structure, file, etc..).
A Mutex semaphore is "owned" by the task that takes it. If Task B attempts to semGive a mutex currently held by Task A, Task B's call will return an error and fail.
Mutexes always use the following sequence:
Here is a simple example:
Binary Semaphore
Binary Semaphore address a totally different question:
Note that with a binary semaphore, it is OK for B to take the semaphore and A to give it.
Again, a binary semaphore is NOT protecting a resource from access. The act of Giving and Taking a semaphore are fundamentally decoupled.
It typically makes little sense for the same task to so a give and a take on the same binary semaphore.