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
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1A plain Mutex serializes everything; even concurrent reads wait for one another.
- 2An RWMutex allows many simultaneous readers but gives writers exclusive access, blocking all readers.
- 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
rust
use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::thread;
Aggregating metrics across threads in Rust
concurrency
shared-state
mutex
Intermediate
7 steps
go
package main import ( "errors"
Parsing and validating CLI flags in Go
cli-parsing
validation
error-handling
Intermediate
8 steps
java
public class ThumbnailProcessor { private static final int MAX_CONCURRENCY = 4;
Bounded parallel thumbnail rendering in Java
concurrency
thread-pool
futures
Intermediate
7 steps
rust
use std::sync::{mpsc, Arc, Mutex}; use std::thread; use std::time::Duration;
Building a thread pool in Rust
concurrency
channels
thread-pool
Advanced
9 steps
go
package cache import ( "container/list"
Building a generic LRU cache in Go
lru-cache
generics
linked-list
Intermediate
8 steps
python
import time from collections import defaultdict from threading import Lock
Sliding-window login rate limiting in Flask
rate-limiting
sliding-window
thread-safety
Intermediate
7 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/mutex-vs-rwmutex-in-go-explained-go-bd57/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.