Skip to content

Commit

Permalink
Go race detector tool
Browse files Browse the repository at this point in the history
  • Loading branch information
aditya43 committed May 31, 2021
1 parent 7716004 commit 08b751b
Showing 1 changed file with 46 additions and 0 deletions.
46 changes: 46 additions & 0 deletions 05-race-detector/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,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()
//----------------------------------------------------

0 comments on commit 08b751b

Please sign in to comment.