Skip to content

Instantly share code, notes, and snippets.

@krishnakumar4a4
Created February 13, 2019 14:29
Show Gist options
  • Save krishnakumar4a4/787496f89440409bc844b99e5a25fbc1 to your computer and use it in GitHub Desktop.
Save krishnakumar4a4/787496f89440409bc844b99e5a25fbc1 to your computer and use it in GitHub Desktop.
check golang sync map about race conditions
package main
import (
"fmt"
"sync"
)
var t sync.Map
func main() {
fmt.Println("Hello, playground")
t.Store("key",0)
var wg sync.WaitGroup
wg.Add(2)
go func() {
for i:=1; i<=100;i++ {
res, ok := t.Load("key")
fmt.Println("res: ", res.(int))
if ok {
t.Store("key", res.(int)+1)
}
}
defer wg.Done()
}()
go func() {
for i:=1; i<=100;i++ {
res, ok := t.Load("key")
fmt.Println("res: ", res.(int))
if ok {
t.Store("key", res.(int)+1)
}
}
defer wg.Done()
}()
wg.Wait()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment