Created
July 17, 2018 13:52
-
-
Save jnormington/a832472edf9fa0db020ebd028b7849b9 to your computer and use it in GitHub Desktop.
Basic example of a go program which panic math/rng because of vet/feed not being thread safe :(
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" | |
"math/rand" | |
"sync" | |
) | |
var ( | |
seed = int64(1531832676684917546) | |
rng = rand.New(rand.NewSource(seed)) | |
workers = 10 | |
) | |
func main() { | |
rand.Seed(seed) | |
var waitGroup sync.WaitGroup | |
waitGroup.Add(workers) | |
res := make(chan int, workers) | |
go func() { | |
for i := 0; i < workers; i++ { | |
go func(idx int) { | |
defer waitGroup.Done() | |
fmt.Println("Started worker:", idx) | |
for i := 0; i < 1000000; i++ { | |
rng.Int63() | |
} | |
res <- 0 | |
fmt.Println("Ended worker:", idx) | |
}(i) | |
} | |
}() | |
waitGroup.Wait() | |
close(res) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment