首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Goroutine缓冲信道死锁

Goroutine缓冲信道死锁
EN

Stack Overflow用户
提问于 2021-12-14 03:33:59
回答 1查看 59关注 0票数 0

在主要的goroutine中,如果我将值发送到超过容量的通道,我就会陷入死锁,但是如果我发送的是不同的goroutine,就不会出现死锁,为什么?

代码语言:javascript
复制
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)
}
EN

回答 1

Stack Overflow用户

发布于 2021-12-14 07:08:13

代码语言:javascript
复制
func main() {
   c := make(chan int, 2)
   for i := 0; i < 3; i++ {
       // Blocking operation
       c <- i
   }
}
代码语言:javascript
复制
By default >>>sends<<< and receives block(!) until both the sender and receiver are ready

结帐- https://gobyexample.com/channels

您的频道上没有接收者,主接收方被封锁。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70343475

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档