Created
September 13, 2011 21:21
-
-
Save kylelemons/1215211 to your computer and use it in GitHub Desktop.
A really bad data race
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 ( | |
"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