Skip to content

Instantly share code, notes, and snippets.

@Jxck
Created February 9, 2014 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jxck/8896868 to your computer and use it in GitHub Desktop.
Save Jxck/8896868 to your computer and use it in GitHub Desktop.
Not Atomic operation on Map
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