简体   繁体   English

为什么我在尝试从一个从不在 goroutine 中接收数据但在 main func 中接收数据的通道中读取时不会出现死锁

[英]Why do I not get a deadlock when trying to read from a channel that never receives data in a goroutine but do in the main func

Why do I get a deadlock here为什么我会在这里陷入僵局

c := make(chan bool)
fmt.Println(<-c)
fmt.Println("done")

and not here而不是在这里

c := make(chan bool)
go func() {
    fmt.Println(<-c)
}()
fmt.Println("done")

I was expecting a deadlock in both cases because both are trying to read from a channel that is never going to receive data.我预计这两种情况都会出现死锁,因为两者都试图从一个永远不会接收数据的通道中读取数据。

The child goroutine is blocked, but the current goroutine can proceed with its execution.子 goroutine 被阻塞,但当前 goroutine 可以继续执行。

You get a deadlock only when all goroutines are blocked at the same time.只有当所有goroutines 同时被阻塞时,才会出现死锁。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM