Skip to content

Instantly share code, notes, and snippets.

@kylelemons
Created September 13, 2011 21:21
Show Gist options
  • Save kylelemons/1215211 to your computer and use it in GitHub Desktop.
Save kylelemons/1215211 to your computer and use it in GitHub Desktop.
A really bad data race
package main
import (
"flag"
"runtime"
)
var (
n = flag.Int("n", 8, "Number of counting functions")
count = flag.Int("max", 1e5, "Number to count down from")
)
func main() {
flag.Parse()
// One for collector and scheduler
runtime.GOMAXPROCS(*n + 2)
done := make(chan bool, *n)
counter := *count
for i := 0; i < *n; i++ {
go func(mod, id int) {
for counter > 0 {
if counter%mod == id {
counter--
}
}
done <- true
}(*n, i)
}
for i := 0; i < *n; i++ {
<-done
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment