Use sync.RWMutex in Go when many goroutines read shared data and writes are comparatively rare, and when the read section is long enough that blocking other readers would hurt. It allows any number of simultaneous readers or exactly one writer. For short critical sections or write-heavy data, a plain sync.Mutex is usually as fast or faster, so a Go RWMutex is not a free upgrade.
The decision should rest on measurement. This guide shows the API, a working example, the costs and the situations where the alternatives win.
An RWMutex has two lock modes. A read lock is shared and a write lock is exclusive.
| Method | Effect |
|---|---|
RLock() / RUnlock() | Take or release a shared read lock |
Lock() / Unlock() | Take or release the exclusive write lock |
TryRLock() / TryLock() | Non-blocking attempts (Go 1.18 and later) |
Many goroutines can hold RLock at once. Lock waits for all readers to release, and while a writer waits, new RLock calls block. That rule stops a steady stream of readers from starving the writer.
Get takes a read lock, so the three goroutines can read concurrently. Set takes the write lock and excludes everyone. Always pair each lock with defer unlock so panics and early returns release it.
Choose it when all of these hold:
runtime.SetMutexProfileFraction) or a benchmark.A typical fit is a configuration or routing table that is read on every request and replaced rarely.
An RWMutex has more bookkeeping than a Mutex. Each RLock updates a shared counter, which can bounce cache lines between cores, so a very short read section may not scale at all.
| Situation | Better choice |
|---|---|
| Short critical sections, mixed reads and writes | sync.Mutex |
| A counter | sync/atomic types such as atomic.Int64 |
| A single value swapped wholesale | atomic.Pointer[T] or atomic.Value |
| Map with stable keys, written once and read many times | sync.Map |
| Many readers, rare writes, immutable snapshots | Copy on write with an atomic pointer |
Copy on write gives readers no locking at all:
Writers build a new Config and call Store. Readers never block, but they must treat the value as read-only.
Lock while holding RLock in the same goroutine deadlocks. Release the read lock first and, after taking the write lock, recheck the condition.RLock twice in one goroutine can deadlock if a writer queues in between, because new readers block behind a waiting writer.go vet flags copies.RLock, even to a field you think is unrelated. The race detector (go test -race) catches it.Only when reads dominate, run in parallel and take long enough to benefit. Otherwise its extra bookkeeping can make it slower, so benchmark with realistic load.
Yes, a mutex is not tied to a goroutine, but it is rarely a good design. Release the lock in the same function that acquires it.
Yes. Once Lock is waiting, later RLock calls wait until the writer finishes. This prevents writer starvation but means readers can be delayed.
No. Use atomic.Int64, which is simpler and faster.
Use sync.RWMutex for read-heavy shared data with longer read sections, and confirm the gain with a benchmark or mutex profile. Default to sync.Mutex for short critical sections and mixed workloads, and reach for atomics or copy on write when you can avoid locking. Remember there is no lock upgrade, avoid recursive read locks, and always release with defer.