pthread_rwlock_wrlock()¶
Purpose¶
pthread_rwlock_wrlock() acquires a write lock on a POSIX reader-writer lock.
The write lock provides exclusive access to the protected data.
While a writer holds the lock:
- No readers may acquire the lock.
- No other writers may acquire the lock.
Header¶
Prototype¶
Parameters¶
rwlock¶
Pointer to an initialized reader-writer lock.
Example:
Return Value¶
Returns:
on success.
Otherwise returns a POSIX error code.
Common errors:
| Error | Description |
|---|---|
| EDEADLK | Deadlock detected |
| EINVAL | Invalid rwlock object |
Usage Pattern¶
Writer Behavior¶
A writer requires exclusive ownership.
Example:
The writer cannot proceed until all readers leave the critical section.
Blocking Behavior¶
A writer blocks when:
or:
Example¶
Typical Use Cases¶
Configuration Updates¶
Shared Lookup Tables¶
Dynamic Data Structures¶
where modifications require exclusive access.
Writer Fairness¶
RWLock implementations must decide how to handle waiting writers.
Common policies:
Reader Preference¶
Advantages:
Disadvantages:
Writer Preference¶
Advantages:
Disadvantages:
Relationship to Day62¶
The Day62 lab explored:
using custom reader-writer lock implementations.
Advantages¶
Higher Read Concurrency¶
Multiple readers can execute simultaneously.
Exclusive Writer Access¶
Writers can safely modify shared data.
Limitations¶
Writer May Wait For All Readers¶
A long-running reader delays writers.
More Complex Than Mutex¶
RWLock introduces fairness and starvation considerations.
Reader Overhead Still Exists¶
Readers still acquire and release locks.
This motivates lockless-read mechanisms such as:
Comparison¶
Mutex¶
RWLock¶
Seqlock¶
Readers do not acquire locks but may retry.
RCU¶
Readers do not acquire locks and focus on safe object lifetime management.