我已經(jīng)做了一段時(shí)間了,但仍然無(wú)法弄清楚為什么會(huì)出現(xiàn)這種死鎖(https://play.golang.org/p/INeUl_ktMJA):package mainimport ( "context" "time")func main() { ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second) t := time.NewTicker(time.Second) for range t.C { select { case <-ctx.Done(): t.Stop() default: print(".") } } cancel()}我希望關(guān)閉的上下文關(guān)閉應(yīng)該退出范圍循環(huán)并因此清理上下文的股票通道。反而:...fatal error: all goroutines are asleep - deadlock!goroutine 1 [chan receive]:main.main() /Users/andig/htdocs/test.go:12 +0xafexit status 2
1 回答

一只名叫tom的貓
TA貢獻(xiàn)1906條經(jīng)驗(yàn) 獲得超3個(gè)贊
它寫(xiě)在 Stop() 的文檔中:
Stop 不會(huì)關(guān)閉通道,以防止從通道讀取的并發(fā) goroutine 看到錯(cuò)誤的“滴答”。
- 1 回答
- 0 關(guān)注
- 194 瀏覽
添加回答
舉報(bào)
0/150
提交
取消