-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
46 lines (39 loc) · 1015 Bytes
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package main
import (
"fmt"
"math/rand"
"time"
)
// identify the data race
// fix the issue.
func main() {
start := time.Now()
reset := make(chan bool)
// var t *time.Timer
t := time.AfterFunc(randomDuration(), func() {
fmt.Println(time.Since(start))
reset <- true
})
for time.Since(start) < 5*time.Second {
<-reset
t.Reset(randomDuration())
}
}
func randomDuration() time.Duration {
return time.Duration(rand.Int63n(1e9))
}
//----------------------------------------------------
// (main goroutine) -> t <- (time.AfterFunc goroutine)
//----------------------------------------------------
// (working condition)
// main goroutine..
// t = time.AfterFunc() // returns a timer..
// AfterFunc goroutine
// t.Reset() // timer reset
//----------------------------------------------------
// (race condition- random duration is very small)
// AfterFunc goroutine
// t.Reset() // t = nil
// main goroutine..
// t = time.AfterFunc()
//----------------------------------------------------