Difference Between Similar Terms and Objects

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.


Search DifferenceBetween.net :

Custom Search



1 Star2 Stars3 Stars4 Stars5 Stars (4 votes, average: 3.50 out of 5)
Loading ... Loading ...


Email This Post Email This Post : If you like this article or our site. Please spread the word. Share it with your friends/family.



See more about : ,

1 Comment

  1. Thanku for posting this article i understood clearly the difference………………:)

Leave a Response

Please note: comment moderation is enabled and may delay your comment. There is no need to resubmit your comment.

Articles on DifferenceBetween.net are general information, and are not intended to substitute for professional advice. The information is "AS IS", "WITH ALL FAULTS". User assumes all risk of use, damage, or injury. You agree that we have no liability for any damages.


Protected by Copyscape Plagiarism Finder