Skip to content
sync Primitives in Depth
step 1/4

Reading — step 1 of 4

Learn

~1 min readConcurrency at Scale

sync.Mutex is the basic lock. The package has more specialized tools.

sync.RWMutex — many readers, one writer:

go

Readers share an RLock; writers need exclusive Lock. Multiple RLocks can be held simultaneously.

sync.Once — guaranteed single execution:

go

Multiple goroutines calling getDB() will see the initialized db — without races, with no risk of double-init.

sync.WaitGroup — wait for N tasks:

go

sync.Pool — reusable object pool, reduces GC pressure:

go

sync.Map — concurrent map (specialized — use only when you really need it). For most cases, map + RWMutex is faster.

sync/atomic — primitives without locks, just CAS instructions:

go

Rule of thumb: for concurrent code, prefer channels for coordination, mutexes for protecting shared state. atomic for hot-path counters where lock overhead matters.

Discussion

Ask a question, share an insight, or help someone who’s stuck.

Sign in to post a comment or reply.

Loading…