定时器实现方案
1、最小堆 golang的定时器
https://github.com/polaris1119/The-Golang-Standard-Library-by-Example/blob/master/chapter04/04.4.md
用法
fmt.Println(time.Now())
f := func(){
fmt.Println(time.Now(),"hello world")
}
time.AfterFunc(time.Second,f)
time.Sleep(2*time.Second)
fmt.Println(time.Now())
/*
2021-01-31 12:10:31.158899599 +0800 CST m=+0.000195036
2021-01-31 12:10:32.159610127 +0800 CST m=+1.000905972 hello world
2021-01-31 12:10:33.159661235 +0800 CST m=+2.000957358
*/
time.AfterFunc定义在src/time/sleep.go
// AfterFunc waits for the duration to elapse and then calls f
// in its own goroutine. It returns a Timer that can
// be used to cancel the call using its Stop method.
func AfterFunc(d Duration, f func()) *Timer {
t := &Timer{
r: runtimeTimer{
when: when(d),
f: goFunc,
arg: f,
},
}
startTimer(&t.r)
return t
}
startTimer定义在src/runtime/time.go
// startTimer adds t to the timer heap.
//go:linkname startTimer time.startTimer
func startTimer(t *timer) {
//...
addtimer(t)
//...
}
// addtimer adds a timer to the current P.
// This should only be called with a newly created timer.
// That avoids the risk of changing the when field of a timer in some P's heap,
// which could cause the heap to become unsorted.
func addtimer(t *timer) {
//...
doaddtimer(pp, t)
//...
}
// doaddtimer adds t to the current P's heap.
// The caller must have locked the timers for pp.
func doaddtimer(pp *p, t *timer) {
//...
siftupTimer(pp.timers, i)
//...
}
最小堆,不同于平常堆排序用的二叉堆,go用的是4叉堆。定义在src/runtime/time.go
// Heap maintenance algorithms.
// These algorithms check for slice index errors manually.
// Slice index error can happen if the program is using racy
// access to timers. We don't want to panic here, because
// it will c