Here is the code I ran:
package main
import (
"fmt"
"time"
)
const delay = 9 * time.Millisecond
func main() {
n := 0
go func() {
time.Sleep(delay)
n++
}()
fmt.Println(n)
}
Here is the command I used:
go run -race data_race_demo.go
Here is the behavior I noticed:
- With
delay
set to 9ms or lower, data race is always detected (program throwsFound 1 data race(s)
) - With
delay
set to 12ms or higher, data race is never detected (program simply prints0
) - With
delay
set to 10 to 11ms, data race occurs intermittently (that is, sometimes prints0
and sometimes throwsFound 1 data race(s)
)
Why does this happen at around 10-11ms?
I'm using Go 1.16.3 on darwin/amd64
, if that matters.