# 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:

```go
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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://ineelhere.gitbook.io/code-with-go/41-mutexes.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
