Skip to content

Instantly share code, notes, and snippets.

@liyzcj
Last active July 5, 2022 16:22
Show Gist options
  • Save liyzcj/1ef3f5ba12b19bb6e18942a71f1ee237 to your computer and use it in GitHub Desktop.
Save liyzcj/1ef3f5ba12b19bb6e18942a71f1ee237 to your computer and use it in GitHub Desktop.
There is a race condition when you want to reuse Timer instance with Reset method.
package main
import "time"
func main() {
for i := 0; i < 10000; i++ {
timer := time.NewTimer(time.Microsecond)
time.Sleep(time.Microsecond)
flat := false
if !timer.Stop() {
select {
case <-timer.C:
default:
flat = true
}
}
if flat {
timer.Reset(5 * time.Second)
select {
case <-timer.C:
println("hello") // this will print immediately
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment