NOTE
2.3 The sync Package
A map of Go's shared-memory synchronization primitives: Mutex, RWMutex, Once, Cond, Map, Pool, WaitGroup, and atomic operations.
This is a historical learning note and may contain outdated or incomplete understanding.
1. What Is sync?
The sync package provides shared-memory synchronization primitives.
Choose the primitive that expresses the invariant clearly:
- Mutex — exclusive access;
- RWMutex — multiple readers or one writer;
Once— run initialization exactly once;Cond— wait for changes to a condition while holding an associated lock;- Map — specialized concurrent map;
Pool— temporary object reuse to reduce allocation pressure;- WaitGroup — wait for a group of tasks to finish.
sync/atomic provides atomic operations for specific lock-free state transitions.
2. Channels vs. sync
The standard-library guidance that higher-level synchronization is often better expressed through channels is useful, but not absolute.
Use channels when the problem is communication/ownership. Use sync when the problem is protecting or coordinating shared state. The clearer correctness model is usually the better choice.
3. Memory Ordering
These primitives do more than prevent simultaneous execution: their documented operations establish happens-before relationships in the Go memory model. Correct concurrent code must reason about both exclusion and visibility.