I'm running a function in a goroutine each time a for-loop iterates, and I'm using sync.WaitGroup
to make sure the goroutines all finish. However, I'm getting weird behavior testing the concurrency with counters. In the example below, I attempt to keep track of the thread count using 4 different techniques (w
, x
, y
, z
), and get 4 different results. The only result I understand is x
, since it is incremented in the for-loop itself. What am I missing here?
package main
import "fmt"
import "sync"
var w = 0
func main() {
x := 0
y := 0
z := 0
var wg sync.WaitGroup
for i := 0; i < 10000; i++ {
wg.Add(1)
x++
go func() {
z++
test(&y)
wg.Done()
}()
}
wg.Wait()
fmt.Println(w, x, y, z) // 8947 10000 8831 8816
}
func test(y *int) {
w++
*y++
}