Mutexes

This Go code illustrates the use of a mutex (Mutual Exclusion) to safely increment counters in a concurrent environment. Here's an explanation with inline comments:

package main

import (
	"fmt"
	"sync"
)

// Container represents a structure with a mutex-protected map of counters.
type Container struct {
	mu       sync.Mutex
	counters map[string]int
}

// inc increments the counter for a given name, protected by a mutex.
func (c *Container) inc(name string) {
	c.mu.Lock()
	defer c.mu.Unlock()
	c.counters[name]++
}

func main() {
	// Initializing a Container with counters for names "a" and "b"
	c := Container{
		counters: map[string]int{"a": 0, "b": 0},
	}

	var wg sync.WaitGroup

	// doIncrement is a function that increments a counter 'n' times for a given name.
	doIncrement := func(name string, n int) {
		for i := 0; i < n; i++ {
			c.inc(name)
		}
		wg.Done()
	}

	// Launching three goroutines to increment counters concurrently.
	wg.Add(3)
	go doIncrement("a", 10000)
	go doIncrement("a", 10000)
	go doIncrement("b", 10000)

	// Waiting for all goroutines to finish.
	wg.Wait()

	// Printing the final counters.
	fmt.Println(c.counters)
}

Output

map[a:20000 b:10000]

Explanation:

  1. Container struct: It includes a mutex (mu) and a map of counters (counters).

  2. inc method: It increments the counter for a given name, using a mutex (mu) to protect concurrent access.

  3. In the main function:

    • An instance of Container (c) is created with counters initialized for names "a" and "b".

    • The doIncrement function is defined to increment a counter 'n' times for a given name. It uses c.inc to safely increment counters.

    • Three goroutines are launched concurrently to increment counters for names "a" and "b".

    • The sync.WaitGroup (wg) is used to wait for all goroutines to finish before proceeding.

    • The final counters are printed.

Using a mutex ensures that the inc method is executed atomically, preventing race conditions that might occur when multiple goroutines attempt to modify the counters concurrently. This approach ensures the correctness and safety of the concurrent counter increments.

Last updated