In Golang’s standard library sync, sync.Mutex is a basic mutual exclusion lock used to protect shared resources and prevent multiple goroutines from simultaneously accessing or modifying data. The sync package provides several types of locks and synchronization mechanisms. Here’s a comprehensive overview of the main lock types and their differences:

1. sync.Mutex (Mutual Exclusion Lock)

sync.Mutex is the most basic lock type used to protect critical sections, ensuring only one goroutine can enter the locked area.

Characteristics

  • Exclusive lock: When a goroutine acquires the lock, other goroutines must wait until it’s released (using Unlock()).
  • Suitable for write operations (modifying shared resources) or scenarios requiring non-concurrent execution.

Usage

var mu sync.Mutex
mu.Lock()   // Lock critical section
// Critical section code
mu.Unlock() // Unlock

Important Notes

Forgetting to release the lock (due to goroutine crash or logic errors) can lead to deadlocks. Does not support reentrant locking: If the same goroutine attempts to acquire the lock again, it will cause a deadlock.

2. sync.RWMutex (Read-Write Lock)

sync.RWMutex supports multiple readers and single writer, ideal for read-heavy scenarios.

Characteristics

Read Lock (RLock): Multiple goroutines can acquire read locks simultaneously. Write locks are blocked when read locks are held. Write Lock (Lock): Exclusive lock; all read and write operations are blocked when a write lock is held. Best for scenarios with significantly more read operations than write operations.

Usage

var rw sync.RWMutex

// Read operation
rw.RLock()   // Acquire read lock
// Critical section code (reading shared resource)
rw.RUnlock() // Release read lock

// Write operation
rw.Lock()   // Acquire write lock
// Critical section code (modifying shared resource)
rw.Unlock() // Release write lock

Important Notes

Like sync.Mutex, failing to release locks leads to deadlocks. Write locks have higher priority than read locks; subsequent read operations are blocked when write operations request the lock.

3. sync.Cond (Condition Variable)

sync.Cond is a synchronization mechanism based on conditions, allowing goroutines to wait until specific conditions are met.

Characteristics

Used for implementing advanced synchronization logic (e.g., producer-consumer pattern). Typically used in conjunction with sync.Mutex.

Usage

var mu sync.Mutex
cond := sync.NewCond(&mu)

go func() {
    mu.Lock()
    cond.Wait() // Wait for condition
    fmt.Println("Condition met")
    mu.Unlock()
}()

mu.Lock()
cond.Signal() // Wake up one waiting goroutine
mu.Unlock()

Important Notes

Wait() must be called after Lock() to avoid runtime errors. Signal() wakes one waiting goroutine, while Broadcast() wakes all waiting goroutines.

4. sync.Once (One-time Execution)

sync.Once ensures a piece of code is executed only once, regardless of how many goroutines attempt to execute it.

Characteristics

Ideal for initialization operations (e.g., singleton pattern). Thread-safe and efficient.

Usage

var once sync.Once

func initFunction() {
    fmt.Println("Initialized")
}

func main() {
    for i := 0; i < 10; i++ {
        go func() {
            once.Do(initFunction) // Ensures single execution
        }()
    }
}

5. sync.Map (Concurrent-safe Map)

sync.Map is Go’s built-in concurrent-safe map implementation using an efficient read-write separation strategy.

Characteristics

No manual locking required; synchronization handled internally. Suitable for read-heavy scenarios.

Usage

var m sync.Map

// Store data
m.Store("key", "value")

// Load data
value, ok := m.Load("key")
if ok {
    fmt.Println(value)
}

// Delete data
m.Delete("key")

Important Notes

Not suitable for frequent write operations as they can decrease efficiency. Consider using regular map with sync.Mutex for high-frequency operations.

6. Custom Locks (Channel-based)

Developers can implement custom locks using channels:

Simple Channel Lock Implementation

type ChanLock struct {
    ch chan struct{}
}

func NewChanLock() *ChanLock {
    return &ChanLock{ch: make(chan struct{}, 1)}
}

func (l *ChanLock) Lock() {
    l.ch <- struct{}{}
}

func (l *ChanLock) Unlock() {
    <-l.ch
}

func main() {
    lock := NewChanLock()
    
    lock.Lock()
    // Critical section code
    lock.Unlock()
}

Differences and Selection Guidelines

sync.Mutex vs sync.RWMutex:

Use sync.RWMutex when most operations are reads for better performance.
Use sync.Mutex when read/write ratios are similar or write operations are frequent.

sync.Cond vs Other Locks:

Use sync.Cond when waiting for specific conditions.
Prefer sync.Mutex or sync.RWMutex for basic critical section protection.

sync.Once vs Manual Control:

sync.Once is optimal for initialization code that should run exactly once.

sync.Map vs Regular Map with Lock:

Consider sync.Map for frequent operations with read-heavy patterns.
Regular map with sync.Mutex might be more flexible and efficient otherwise.