Reader-Writer Lock Fairness and Starvation¶
Overview¶
Reader-writer lock fairness policy directly affects:
- reader throughput
- writer latency
- starvation behavior
- synchronization scalability
The most important fairness question is:
Different answers create different rwlock policies.
Reader Preference¶
Concept¶
Reader-preference rwlock allows new readers to acquire lock even when a writer is already waiting.
Example:
Reader Preference Policy Example¶
Example condition:
Meaning:
| Writer State | Reader Allowed |
|---|---|
| WRITER_FREE | Yes |
| WRITER_WAIT_QUEUE | Yes |
| WRITER_ACQUIRED | No |
Advantages of Reader Preference¶
Benefits:
- high reader throughput
- high read-side concurrency
- low reader latency
Useful for:
- read-heavy workloads
- frequently accessed shared data
- cache lookup systems
Disadvantages of Reader Preference¶
Main problem:
Because:
Writer may wait for very long time.
Observed behavior:
Writer Starvation¶
Starvation Definition¶
Starvation means:
In reader-preference rwlock:
- readers continuously acquire lock
- writer cannot obtain exclusive ownership
Observed Starvation Behavior¶
Experimental observation:
while readers continuously acquired and released read lock.
This demonstrates classic rwlock starvation tendency.
Writer Preference¶
Concept¶
Writer-preference rwlock blocks new readers once a writer begins waiting.
Example:
Writer Preference Policy Example¶
Example condition:
Meaning:
| Writer State | Reader Allowed |
|---|---|
| WRITER_FREE | Yes |
| WRITER_WAIT_QUEUE | No |
| WRITER_ACQUIRED | No |
Advantages of Writer Preference¶
Benefits:
- lower writer latency
- reduced starvation risk
- more predictable writer progress
Useful for:
- write-sensitive systems
- latency-sensitive updates
- systems requiring stronger fairness
Disadvantages of Writer Preference¶
Main tradeoff:
Because:
- readers blocked earlier
- read concurrency reduced
- more synchronization stalls
Fairness Tradeoff¶
RWLock fairness always involves tradeoffs.
| Policy | Reader Throughput | Writer Latency | Starvation Risk |
|---|---|---|---|
| Reader Preference | High | High | High |
| Writer Preference | Lower | Lower | Lower |
There is no perfect policy.
The correct design depends on workload characteristics.
RWLock Fairness Is a Policy Decision¶
The most important observation:
This single design decision heavily affects:
- scalability
- throughput
- latency
- starvation behavior
Busy Spinning and Fairness¶
Simplified spin rwlocks commonly use:
for waiting.
Problems:
- busy spinning
- scheduler overhead
- CPU waste
Fairness problems become more visible under heavy contention.
Cacheline Contention¶
RWLocks also introduce shared-state contention.
Example:
All readers continuously modify the same atomic counter.
This creates:
- cacheline bouncing
- inter-core synchronization traffic
- scalability bottlenecks
Especially problematic on multi-core systems.
Why RWLock Design Is Difficult¶
RWLock complexity comes from:
- reader/writer coordination
- ownership transition
- fairness policy
- starvation prevention
- scalability tradeoffs
The problem is much more complicated than simple locking.
Motivation for Advanced Synchronization¶
RWLock limitations motivated more scalable synchronization mechanisms.
Examples:
- seqlock
- RCU
- lock-free synchronization
These mechanisms reduce:
- reader overhead
- cacheline contention
- fairness bottlenecks
especially for read-heavy workloads.
Reader Preference vs Writer Preference Summary¶
Reader Preference¶
Writer Preference¶
Summary¶
RWLock fairness policy is one of the most important synchronization design decisions.
The core tradeoff is:
Understanding fairness and starvation behavior is essential for designing scalable synchronization systems.