Created
February 9, 2014 09:53
-
-
Save Jxck/8896868 to your computer and use it in GitHub Desktop.
Not Atomic operation on Map
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 ( | |
"log" | |
"runtime" | |
"sync" | |
) | |
var debug func(a ...interface{}) | |
func init() { | |
log.SetFlags(log.Lshortfile) | |
debug = func(a ...interface{}) { | |
log.Printf("%+v", a) | |
} | |
debug(runtime.NumCPU()) | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
} | |
func main() { | |
var wg sync.WaitGroup | |
Map := make(map[int]int) | |
for i := 0; i < 100000; i++ { | |
wg.Add(1) | |
go func() { | |
defer wg.Done() | |
j := Map[0] | |
j = j + 1 | |
Map[0] = j | |
actual := Map[0] | |
expected := j | |
if actual != expected { | |
debug(actual, expected) | |
} | |
}() | |
} | |
wg.Wait() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment