Code with Go
About
  • Code With Go! 🚀
  • Hello World
  • Values
  • Variables
  • Constants
  • For Loop
  • If Else
  • Switch
  • Array
  • Slice
  • Map
  • Range
  • Functions
  • Multiple Return Values
  • Variadic Functions
  • Closure
  • Recursion
  • Pointers
  • Strings and Runes
  • Structs
  • Methods
  • Interfaces
  • Struct Embedding
  • Generics
  • Error
  • Goroutines
  • Channels
  • Channel Buffering
  • Channel Synchronization
  • Channel Directions
  • Select
  • Timeouts
  • Non-Blocking Channel Operations
  • Closing Channels
  • Range over Channels
  • Timers
  • Tickers
  • Worker Pools
  • Wait Groups
  • Rate Limiting
  • Atomic Counters
  • Mutexes
  • Stateful Goroutines
  • Sorting
  • Sorting by Functions
  • Panic
  • Defer
  • Recover
  • String Functions
  • String Formatting
  • Text Templates
  • Regular Expressions
  • JSON
  • XML
  • Time
  • Epoch
  • Time Formatting / Parsing
  • Random Numbers
  • Number Parsing
  • SHA256 Hashes
  • Base64 Encoding
  • Reading Files
  • Writing Files
  • Line Filters
  • File Paths
  • Directories
  • Temporary Files and Directories
  • Embed Directive
  • Testing and Benchmarking
  • Command-Line Arguments
  • Command-Line Flags
  • Command-Line Subcommands
  • Environment Variables
  • Logging
  • HTTP Client
  • HTTP Server
  • Context
  • Spawning Processes
  • Exec'ing Processes
  • Signals
  • Exit
  • Resize Image
Powered by GitBook
On this page

Was this helpful?

Edit on GitHub

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.

PreviousAtomic CountersNextStateful Goroutines

Last updated 1 year ago

Was this helpful?