[英]Goroutine buffered channel deadlock
In the main goroutine, I'm getting a deadlock if I send values to channel exceeded the capacity, but if I'm sending in a different goroutine, there will be no deadlock, Why?在主 goroutine 中,如果我向通道发送超出容量的值,我会遇到死锁,但是如果我在不同的 goroutine 中发送,就不会出现死锁,为什么?
func main() {
c := make(chan int, 2)
for i := 0; i < 3; i++ {
c <- i
}
}
func main() {
c := make(chan int, 2)
go func() {
for i := 0; i < 3; i++ {
c <- i
}
close(c)
}()
time.Sleep(5 * time.Second)
}
func main() {
c := make(chan int, 2)
for i := 0; i < 3; i++ {
// Blocking operation
c <- i
}
}
By default >>>sends<<< and receives block(!) until both the sender and receiver are ready
Checkout - https://gobyexample.com/channels结帐 - https://gobyexample.com/channels
There are no receivers on your channel, main goroutine(= sender) is blocked您的频道上没有接收者,主 goroutine(= 发送者)被阻止
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.