Mutex — exclusive lock. RWMutex — concurrent reads, exclusive writes.
sync.Mutex:
Lock()/Unlock() — one at a time.sync.RWMutex:
RLock()/RUnlock() — concurrent reads.Lock()/Unlock() — exclusive writes.1// Mutex2var mu sync.Mutex3mu.Lock()4defer mu.Unlock()56// RWMutex7var rwmu sync.RWMutex8rwmu.RLock()9defer rwmu.RUnlock()
When to use RWMutex:
When to use Mutex:
Common mistakes:
defer Unlock().