Can a Semaphore act as a Mutex?

ANSWER: Let’s define both these terms.
Semaphore: A semaphore control access to a shared resource using a counter. The counter measures the number of permits that are allowed. If a counter is zero, then access to the resource is denied. If it is greater than zero, then access to the resource allowed. Every time a thread acquires a resource, the counter is decremented. When it is done using the resource, it releases the resource and the counter is incremented. Lastly, when the counter is zero, any thread that needs access to the resource has to wait for another thread to release the resource.

Mutex: Mutex stands for mutual exclusion. When only one thread is allowed to have access to the thread at a time, it is called a mutex. Another thread has to wait for the resource to be freed in order to use it.
Important note: A semaphore can be a mutex when the semaphore counter permits only one resource. In this case, there is only one resource is available for allocation at that specific time.

Leave a comment

Design a site like this with WordPress.com
Get started