Created
February 13, 2019 14:29
-
-
Save krishnakumar4a4/787496f89440409bc844b99e5a25fbc1 to your computer and use it in GitHub Desktop.
check golang sync map about race conditions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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