Reader/Writer Locks in C
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
A reader-writer lock lets many threads read shared data at the same time while still ensuring that writers get exclusive access. In C, the usual primitive is pthread_rwlock_t, and it is most useful when reads are frequent, writes are relatively rare, and a plain mutex would create unnecessary contention.
Why Use a Reader-Writer Lock
A normal mutex allows only one thread into the protected section at a time, regardless of whether the work is read-only. That is simple and safe, but it can waste concurrency when multiple readers could have proceeded together.
A reader-writer lock separates those cases:
- multiple readers may hold the lock simultaneously
- only one writer may hold it
- no reader may hold it while a writer is active
This makes the primitive attractive for caches, lookup tables, and shared configuration state.
The POSIX API in C
The POSIX threads API provides the standard operations:
- '
pthread_rwlock_init' - '
pthread_rwlock_rdlock' - '
pthread_rwlock_wrlock' - '
pthread_rwlock_unlock' - '
pthread_rwlock_destroy'
A minimal example looks like this:
Compile it with:
This is the normal C pattern. Readers take the lock in shared mode, writers take it in exclusive mode, and both release it with pthread_rwlock_unlock.
Choosing Read Lock Versus Write Lock
Use a read lock only when the protected code truly does not modify shared state. If a thread might write, even indirectly, it must take the write lock.
That includes seemingly harmless operations such as:
- lazy initialization of cached fields
- statistics counters stored in the same shared structure
- mutation through function calls hidden behind a read-looking API
If the protected data changes, the thread is a writer from the lock's point of view.
Fairness and Starvation
Reader-writer locks can improve throughput, but they introduce policy concerns that ordinary mutexes avoid. Some implementations favor readers, some favor writers, and some balance between the two.
That matters because a read-heavy workload can starve writers if new readers keep arriving. Conversely, a writer-preference implementation can reduce reader throughput once a writer starts waiting.
This is why reader-writer locks are not automatically better than mutexes. They trade simple exclusion for a more complex access policy.
Performance Is Workload-Dependent
A reader-writer lock is worthwhile only when concurrent reads are common enough to offset its extra complexity. If the critical section is tiny or writes happen frequently, a plain mutex may be simpler and faster.
Use a reader-writer lock when:
- many threads mostly read shared state
- write operations are infrequent
- the protected section is large enough that concurrent reading actually helps
Do not choose it just because it sounds more advanced.
Common Pitfalls
- Taking a read lock around code that actually mutates shared state.
- Assuming reader-writer locks are always faster than mutexes.
- Forgetting that writer starvation can happen depending on the implementation and workload.
- Protecting very small critical sections where the extra complexity buys little.
- Mixing unlocked access with locked access and then blaming the lock primitive for race conditions.
Summary
- In C, reader-writer locks are commonly implemented with
pthread_rwlock_t. - Multiple readers can proceed together, but writers need exclusive access.
- They are best for read-heavy workloads with relatively infrequent writes.
- Correctness depends on using read and write modes honestly.
- A plain mutex may still be the better choice when the workload is small or write-heavy.
Related reading
- ReaderWriterLock vs lock
- ReaderWriterLock vs lock
- ReadFile doesn't work asynchronously on Win7 and Win2k8
- Reading asynchronous pipe - loosing data
- Reading a C/C data structure in C from a byte array
- Reading asynchronously from stdin with Qt
- Recover an Asynch ThreadPoolTaskexecutor after server crashed/shut down
- Recursive Lock Mutex vs Non-Recursive Lock Mutex
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.