Mutex Vs Semaphore Vs Spinlock

All three are used for protecting the critical section for concurrent access.

When we use Spinlock ?

Spinlock is not wasteful where it makes sense. For very small critical sections, it would be wasteful to allocate a mutex task queue compared to simply suspending the scheduler for a few microseconds that it takes to finish the important work. 

Grab a spinlock when you need simple mutual exclusion for blocks of memory transactions. So spinlocks tend to be used in situations where they will remain held for a very short time, so that most attempts are expected to succeed on the first try, and those that need a wait don't wait long.

Use a spinlock when you really want to use a mutex but your thread is not allowed to sleep. e.g.: An interrupt handler within OS kernel must never sleep. If it does the system will freeze / crash. If you need to insert a node to globally shared linked list from the interrupt handler, acquire a spinlock - insert node - release spinlock. 

Spinlock will be used in both interrupt and process context. 

Click here to know more about Spinlock.


When we use Mutex ?

Use a mutex when your thread wants to execute code that should not be executed by any other thread at the same time. How can we avoid other threads not to enter into the critical section of the code? Click here. Mutex 'down' happens in one thread and mutex 'up' must happen in the same thread later on. e.g.: If you are deleting a node from a global linked list, you do not want another thread to muck around with pointers while you are deleting the node. When you acquire a mutex and are busy deleting a node, if another thread tries to acquire the same mutex, it will be put to sleep till you release the mutex. When your thread wants to sleep or hold the lock across an io operation, then use a mutex. 
Grab a mutex when you want multiple threads to stop right before a mutex lock and then the highest priority thread to be chosen to continue when mutex becomes free and when you lock and release in the same thread. System hang issue is debugged here.

Spin Lock (Remember)

Mutex

It waits/spins indefinitely in a loop, so it is called spin lock. 

It doesn’t wait, so it is called sleeping lock

Thread will be active, so it will be especially used for interrupt handling.

Thread will not be active as it will go to sleep and give room for other threads.

Suspends/disables other interrupts until it finishes its important work.

It doesn’t suspend interrupts so other processes/threads can take the CPU due to preemption. 

No queue because it already disabled all the interrupts. So, no one can preempt. 

Thread will wait in queue until it acquires the resource.

No context switching because one thread/process owns the CPU until it finishes.

Context switching will happen when an interrupt takes the resource and releases it. It will switch from interrupt to process context and thread will start from where it left.

No preemption as this doesn’t allow any other thread/process. 

Since it gives room for other thread, preemption is allowed

Can be used either in process context or interrupt context. 

Can be used only in process context. 

1) Useful in small critical sections where allocating mutex and queue is costly. This conveys that it will not be useful in multi-threading.  


2) Useful in transient situations

1) When multiple threads want to access critical section, the thread with the highest priority gets the access. 



When we use Semaphore ?

Semaphore is different from Mutex. Click here to know more.
Grab a semaphore when you intend to post it in one thread or an interrupt and take it in another thread. It's three slightly different ways to ensure mutual exclusion and they are used for slightly different purposes.

Use a semaphore when your thread wants to sleep till some other thread tells you to wake up. Semaphore 'down' happens in one thread (producer) and semaphore 'up' (for same semaphore) happens in another thread (consumer) e.g.: In producer-consumer problem, producer wants to sleep till at least one buffer slot is empty - only the consumer thread can tell when a buffer slot is empty.


Click here to know more about Semaphore.

Comments