C shared_mutex implementation
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
std::shared_mutex is a reader-writer lock in C++ that allows many readers or one writer. It is useful when reads are frequent, writes are rare, and a plain std::mutex would force unnecessary serialization.
The name in this article says "C shared_mutex," but the standard type is a C++ feature from C++17. If your compiler or standard library does not provide it, the practical alternatives are std::shared_timed_mutex in C++14 or a platform primitive such as pthread_rwlock_t.
What std::shared_mutex Does
A normal mutex has one mode: locked or unlocked. A shared mutex has two lock modes:
- shared mode for readers
- exclusive mode for writers
Multiple threads can hold the shared lock at the same time, but once a writer takes the exclusive lock, no other reader or writer may enter.
This pattern works well for caches, configuration snapshots, and lookup tables that are read far more often than they are modified.
Basic Usage in C++17
You need the header shared_mutex and either std::shared_lock for readers or std::unique_lock for writers.
The important detail is that readers use std::shared_lock, not std::lock_guard. A write operation must use exclusive locking because it mutates shared state.
When It Helps and When It Does Not
std::shared_mutex is not automatically faster than std::mutex. It helps only when:
- the protected data is read much more often than written
- read sections are not extremely short
- contention is high enough that reader parallelism matters
If writes are frequent, the shared mutex can be slower because the implementation must manage two lock modes and fairness rules. For tiny critical sections with little contention, a plain mutex is often simpler and just as fast.
If std::shared_mutex Is Missing
Some toolchains support C++14 but not full C++17. In that case, std::shared_timed_mutex is a common replacement:
If you are actually writing C rather than C++, POSIX provides pthread_rwlock_t, which solves the same reader-writer problem at the OS API level.
Implementation Notes
At a high level, a shared mutex implementation tracks:
- whether an exclusive owner exists
- how many shared owners exist
- which waiters are blocked for shared or exclusive access
Real implementations also need policies for fairness. If readers are allowed to stream in forever, a waiting writer can starve. If writers are given strong preference, readers can stall badly under moderate write load.
That is why rolling your own reader-writer lock is usually a bad trade unless you have very specific requirements. The standard library or platform runtime has already dealt with memory ordering, wake-up behavior, and starvation tradeoffs.
Compile Example
Compile with C++17 support and threading enabled:
If your compiler reports that std::shared_mutex is missing, try:
and switch the code to std::shared_timed_mutex.
Common Pitfalls
The most common mistake is using the wrong lock type. Readers need std::shared_lock, while writers need std::unique_lock or another exclusive lock wrapper. If you use exclusive locks everywhere, you lose the benefit of the shared mutex.
Another mistake is assuming it belongs to C rather than C++. Standard shared_mutex is not a C language feature. In C, use a platform reader-writer lock such as pthread_rwlock_t.
Developers also overestimate the performance win. Measure before and after. If your workload is write-heavy or the critical section is tiny, std::mutex may be the better choice.
Finally, do not read shared state outside the lock and then act on it later as though it were still valid. A shared mutex protects data only for the lifetime of the lock object.
Summary
- '
std::shared_mutexis a C++ reader-writer lock, not a standard C feature.' - It allows many readers or one writer at a time.
- Use
std::shared_lockfor reads andstd::unique_lockfor writes. - It is most useful for read-heavy workloads with meaningful contention.
- If unavailable,
std::shared_timed_mutexorpthread_rwlock_tare practical fallbacks. - Do not assume it is faster than
std::mutexwithout measuring your workload.

