[英]does a goroutine sending to an unbuffered channel without receivers block forever, after the function that spawns the goroutine returns?
[英]Does this goroutine leak/block forever?
對以下行為感到好奇
func test() error {
ctx, cancel := context.WithCancel(context.Background())
cancel()
doneChan := make(chan bool)
go func() {
// emulate a long running function
time.Sleep(time.Minute)
// never exits?
doneChan <- true
}()
select {
case <- ctx.Done():
return ctx.Err()
case <- doneChan:
return nil
}
}
給定上面的功能,如果select
語句選擇上下文取消,那么goroutine是否試圖永遠推入doneChan
阻塞? 在這種情況下,是否始終僅提供一個緩沖通道的解決方案?
要結束這個問題...簡單的答案是。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.