Last active
July 5, 2022 16:22
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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