Skip to content

Instantly share code, notes, and snippets.

@jnormington
Created July 17, 2018 13:52
Show Gist options
  • Save jnormington/a832472edf9fa0db020ebd028b7849b9 to your computer and use it in GitHub Desktop.
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 :(
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