Locking : Mutex vs Spinlocks

Locking : Mutex vs Spinlocks

What is difference between mutex and spin-lock? First of all, let's understand what is a mutex?

Mutex is kind of a key to use any resources in the system. If you have mutex, you can use the resource, if you don't wait till the mutex is released. Process goes to wait queue for that particular resource.

What is spin-lock? Spin lock is a mechanism in which the process needing a resource poll the lock on resource till it gets it. It is also called as busy-waiting. Process will be busy in a loop till it gets the resource.

So we get the first difference there itself, the way both wait for the resource lock. In case of mutex, process goes in wait state and does not use processor while in spin-lock, process keeps on looping and hence uses the processor even while waiting.

Second difference comes from : when should we use spin-lock or mutex?

Spin-locks are best used when a piece of code cannot go to sleep state. Best example of such code would be interrupts request handlers.

Mutexes are best used in user space program where sleeping of process does not affect the system in catastrophic way.

Spin locks make sense when we have multi-processor systems or we have uni-processor system with preemption enabled, spin locks used in uni-processor systems without preemption enabled, may lead to deadlock where process goes on spinning forever.

Following table summarizes the above points.

Criteria
Mutex
Spinlock
Mechanism
Test for lock.
If available, use the resource
If not, go to wait queue
Test for lock.
If available, use the resource.
If not, loop again and test the lock till you get the lock
When to use
Used when putting process is not harmful like user space programs.
Use when there will be considerable time before process gets the lock.
Used when process should not be put to sleep like Interrupt service routines.
Use when lock will be granted in reasonably short time.
Drawbacks
Incur process context switch and scheduling cost.
Processor is busy doing nothing till lock is granted, wasting CPU cycles.

added 10 years 7 months ago

Contents related to 'Locking : Mutex vs Spinlocks'

Difference between Mutex and Semaphore: This page explains the differences between Mutex vs Semaphore, and describes when to use mutex and when to use semaphore?

Description of Lock, Monitor, Mutex and Semaphore: You can learn the definition of Lock, Monitor, Mutex, Semaphore and see source code examples for concurrent programming.

How does a mutex work? What does a mutex cost?: We will explain how mutex works in programming languages and the cost of employing a mutex in your code.

- Average Calculator
- Most frequently used Linux commands 5
- Most frequently used Linux commands 4
- Most frequently used Linux commands 3
- Most frequently used Linux commands 2
- Most frequently used Linux commands 1
- How to rename a filename to current date with batch
- How does a mutex work? What does a mutex cost?
- Locking : Mutex vs Spinlocks
- Description of Lock, Monitor, Mutex and Semaphore
- Difference between Mutex and Semaphore
- Difference between association, aggregation and composition
- Coordinating C/C++ ØMQ and .NET ØMQ
- Regular Expressions (Regex) Reference Sheet