44. Go - Goroutine Race Conditions & Mutex
Race Condition Example:
package main
import "fmt"
func main() {
counter := 0
for i := 0; i < 100; i++ {
go func() {
temp := counter
temp++
counter = temp
}()
}
fmt.Println(counter)
}
The counter never reaches 100 (100 goroutines counting suppose to increment count by 1) because each goroutine access the counter value at different point in time and then resets the counter where other goroutines will access later.
package main
import (
"fmt"
"time"
)
func main() {
counter := 0
for i := 0; i < 101; i++ {
go func() {
temp := counter
temp++
counter = temp
}()
fmt.Println(counter)
time.Sleep(time.Millisecond * 50)
}
fmt.Println(counter)
}
Adding Sleep Helps.
package main
import (
"fmt"
"sync"
)
func main() {
counter := 0
var mu sync.Mutex
var wg sync.WaitGroup
wg.Add(100)
for i := 0; i < 100; i++ {
go func() {
mu.Lock()
temp := counter
temp++
counter = temp
mu.Unlock()
wg.Done()
}()
//fmt.Println(counter)
}
wg.Wait()
fmt.Println(counter)
}
Using Mutex and WaitGroups allows counter goroutines to reach 100.
Mutex locks variable from usable from other goroutines who are also using same variable.
// sync.Mutex
// mu.Lock()
// mu.Unlock()
Flexible Mutex for Locking Reading or Writing
// type RWMutex
WaitGroup
// sync.WaitGroup
// wg.Add(INT)
// wg.Done()
// wg.Wait()
Comments
Post a Comment