Difference Between Mutex and Semaphore
Mutex vs Semaphore
A mutex is analogous to a single key to a room. A person holding the key, which is analogous to a thread, is the only one who can have access to the room. The person with the access will then have to give up the key to the next person in line. Therefore, a mutex can only be released by the thread that acquires it.
A mutex is normally used to serialize the access to a section of a reentrant code ‘“ a kind of code which is not able to be executed by several threads at once. Only a single thread is allowed into a section. This forces the other threads in queue to wait. Before a thread gains access, it will have to wait until the thread before it gives up the section.
Using the same analogy in mutex, semaphores are the number of similar keys that can access the same number of rooms with similar locks. A semaphore or the value of a semaphore count will depend on the number of people (threads) who enter or exit from the room. If there are 5 rooms and they are all occupied, then the semaphore count is zero. If two leave the room, then the count is two and the two keys are given to next two in the queue.
With that being said, semaphores can be concurrently signaled by any thread or process and are ideal for applications that require synchronization. Nevertheless, semaphores are used to effectively restrict the number of concurrent users of a common resource based on the maximum semaphore count.
So basically, a mutex can be considered as a semaphore having a value of one.
The decrement and increment of the semaphore are dependent on whether threads are requesting access to the common resource or leaving the section.
In theory, mutex and (binary) semaphores are semantically similar.The implementation of the mutex can be done using semaphores and so is the other way around. However, in the practical sense, they can be a bit different.
Mutexes are intended to be applied for mutual exclusion only and binary semaphores are intended to be used for mutual exclusion and event notification. Although they are very similar in terms of implementation and general semantics, they are used differently.
Summary:
1. Mutex is typically used to serialize access to a common resource while a semaphore is a number of concurrent accesses.
2. Mutex is like a semaphore with a count of one.
3. Mutex only allows a single thread to have access while semaphores can be concurrently signaled by any thread or process.
4. Semaphores are ideal for synchronization and often used for event notification and mutual exclusion while mutex is only applied for mutual exclusion.
- Difference Between Lymphoma and Leukemia - March 7, 2024
- Difference Between CP and CPK - June 8, 2010
- Difference Between PPTP and L2TP - June 7, 2010
Search DifferenceBetween.net :
Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.
Thanku for posting this article i understood clearly the difference………………:)