《Go in action》's sample about race conditions:
var (
counter int
wg sync.WaitGroup
)
func main() {
wg.Add(2)
go incCounter(1)
go incCounter(2)
wg.Wait()
fmt.Println("Final Counter:", counter)
}
func incCounter(id int) {
defer wg.Done()
for count := 0; count < 2; count++ {
value := counter
//1 fmt.Println("value=",value)
runtime.Gosched()
value++
counter = value
//2 fmt.Println("counter=",counter)
}
}
it is said Final Counter should be 2 in the end,here is explain: "Each goroutine overwrites the work of the other. This happens when the goroutine swap is taking place. Each goroutine makes its own copy of the counter variable and then is swapped out for the other goroutine. When the goroutine is given time to execute again, the value of the counter variable has changed, but the goroutine doesn’t update its copy. Instead it continues to increment the copy it has and set the value back to the counter variable, replacing the work the other goroutine performed."
I guess it's environment reason,my machine output 4 with go 1.10.3+win10. I want to know what the go changed since the book released? And if I uncomment mark 1 Final Counter prints 2 or random if I uncomment mark 2. why?