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.
Spinlock will be used in both interrupt and process context.
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.
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.
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
Post a Comment