Mutex Locks (Mutual Exclusion Locks)
.
Mutex Locks (Mutual Exclusion Locks)
Why Mutex Locks Are Needed
Hardware-based synchronization mechanisms (like test-and-set and compare-and-swap) are:
-
Low-level
-
Complex
-
Not easily accessible to application programmers
What Is a Mutex Lock?
A mutex (mutual exclusion) lock ensures that only one process or thread can execute a critical section at any given time.
The idea is simple:
-
A thread must acquire the lock before entering the critical section
-
It must release the lock when leaving
This prevents race conditions on shared data.
Basic Usage Pattern
-
acquire()→ requests access to the critical section -
release()→ gives up access so another thread may enter
How a Mutex Lock Works Internally
A mutex lock uses a boolean variable (commonly called available):
-
true→ lock is free -
false→ lock is held by another thread
acquire() Operation
-
If the lock is available, the thread acquires it
-
If not, the thread spins (repeatedly checks)
release() Operation
-
Makes the lock available for other threads
⚠️ Both acquire() and release() must be atomic, typically implemented using CAS or similar hardware instructions.
Lock Contention
A lock can be:
🔹 Uncontended
-
Lock is available when requested
-
Thread acquires it immediately
-
Best-case performance
🔹 Contended
-
Lock is already held
-
Thread must wait
Contended locks can be:
-
Low contention → few threads competing
-
High contention → many threads competing
📉 High contention reduces performance, as many threads waste time waiting.
Busy Waiting and Spinlocks
What Is Busy Waiting?
Busy waiting occurs when a thread:
-
Repeatedly checks if a lock is available
-
Does not give up the CPU while waiting
This wastes CPU cycles.
Spinlocks
A mutex lock that uses busy waiting is called a spinlock.
Why Use Spinlocks at All?
Spinlocks have an important advantage:
-
No context switch is required
Context switches are expensive operations.
If the lock is held for a very short duration, spinning can be faster than:
-
Blocking the thread
-
Switching contexts twice (sleep + wake)
When Is a Lock Held for a “Short Duration”?
A general rule:
Use a spinlock if the lock will be held for less time than two context switches.
This makes spinlocks ideal for:
-
Multicore systems
-
Kernel-level code
-
Very short critical sections
Spinlocks on Multicore Systems
On multicore processors:
-
One thread can spin on one core
-
Another thread executes the critical section on a different core
This makes spinlocks efficient and practical, which is why:
-
Modern operating systems use spinlocks extensively
Advantages and Disadvantages of Mutex Locks
✅ Advantages
-
Simple to use
-
Effective at preventing race conditions
-
Widely supported (e.g., Pthreads)
-
Spinlocks avoid context switch overhead
❌ Disadvantages
-
Busy waiting wastes CPU cycles
-
Poor performance under high contention
-
Not suitable for long critical sections
Final Summary
-
Mutex locks are high-level synchronization tools that ensure mutual exclusion
-
They protect critical sections by enforcing lock acquisition and release
-
Spinlocks are mutex locks that rely on busy waiting
-
Best suited for short-duration locks on multicore systems
-
Mutex locks form the foundation for solving many classical synchronization problems
Comments
Post a Comment