Reading — step 1 of 4
Learn
sync.Mutex is the basic lock. The package has more specialized tools.
sync.RWMutex — many readers, one writer:
Readers share an RLock; writers need exclusive Lock. Multiple RLocks can be held simultaneously.
sync.Once — guaranteed single execution:
Multiple goroutines calling getDB() will see the initialized db — without races, with no risk of double-init.
sync.WaitGroup — wait for N tasks:
sync.Pool — reusable object pool, reduces GC pressure:
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:
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…