[英]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.