go 46 lines · 6 steps

Mutex vs RWMutex in Go

When reads vastly outnumber writes, an RWMutex lets readers run concurrently instead of serializing every access.

Explained by highlit
1package main
2 
3import "sync"
4 
5// Counter uses a plain Mutex: every operation, read or write,
6// takes the same exclusive lock.
7type Counter struct {
8 mu sync.Mutex
9 count int
10}
11 
12func (c *Counter) Inc() {
13 c.mu.Lock()
14 c.count++
15 c.mu.Unlock()
16}
17 
18func (c *Counter) Value() int {
19 c.mu.Lock()
20 defer c.mu.Unlock()
21 return c.count
22}
23 
24// Cache uses an RWMutex: many readers can hold RLock concurrently,
25// while writers take the exclusive Lock and block everyone.
26type Cache struct {
27 mu sync.RWMutex
28 data map[string]int
29}
30 
31func NewCache() *Cache {
32 return &Cache{data: make(map[string]int)}
33}
34 
35func (c *Cache) Get(key string) (int, bool) {
36 c.mu.RLock()
37 defer c.mu.RUnlock()
38 v, ok := c.data[key]
39 return v, ok
40}
41 
42func (c *Cache) Set(key string, val int) {
43 c.mu.Lock()
44 defer c.mu.Unlock()
45 c.data[key] = val
46}
01 / 01
STEP 01

Walkthrough

Space play step click any line
Three takeaways
  1. 1A plain Mutex serializes everything; even concurrent reads wait for one another.
  2. 2An RWMutex allows many simultaneous readers but gives writers exclusive access, blocking all readers.
  3. 3Reach for RWMutex only in read-heavy workloads, since its bookkeeping costs more than a plain Mutex under contention-light or write-heavy use.

Related explainers

Share this explainer

Here's the card — post it anywhere.

Mutex vs RWMutex in Go — share card
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code