Share & grow the world's code base!

Delve into a community where programmers unite to discover code snippets, exchange skills, and enhance their programming proficiency. With abundant resources and a supportive community, you'll find everything essential for your growth and success.

1 snippets
  • Example of using mutexes in Go

    package main
    import (
    // without using mutex the following error can happen: "fatal error: concurrent map writes" while changing the map in multiple goroutines
    type Storage struct {
    	storage map[string]int // map is not threade-safe
    func (s *Storage) Increment(name string) {
    	s.Lock() // lock the mutex before accessing counters
    	defer s.Unlock() // unlock the mutex at the end of the function using a defer statement[name]++
    func (s *Storage) Decrement(name string) {
        defer s.Unlock()[name]--
    func main() {
    	s := &Storage{
    		storage: make(map[string]int),
    	wg := sync.WaitGroup{}
        // increment a named counter in a loop
    	increment := func(name string, count uint) {
    		for i := 0; i < int(count); i++ {
        // decrement a named counter in a loop
    	decrement := func(name string, count uint) {
    		for i := 0; i < int(count); i++ {
        // run 5 goroutines concurrently
    	go increment("a", 1000)
    	go decrement("a", 500)
    	go increment("b", 1000)
    	go increment("b", 1000)
    	go decrement("b", 1500)
    	wg.Wait() // wait for the goroutines to finish
    // $ go run main.go  
    // map[a:500 b:500]
    // without mutex the following error can happen
    // $ go run main.go
    // fatal error: concurrent map writes

    Mutexes can be used to safely access data across multiple goroutines.This example shows how to use mutexes in Go to change a map concurrently and safely.