为了账号安全,请及时绑定邮箱和手机立即绑定

等待组。等待() 导致死锁

等待组。等待() 导致死锁

Go
胡子哥哥 2022-09-12 20:59:48
我试图弄清楚为什么我有一个带有等待组的死锁。等待()package mainimport (    "fmt"    "sync")var wg sync.WaitGroupfunc foo(c chan int, i int) {    defer wg.Done()    c <- i}func main() {    ch := make(chan int)    for i := 0; i < 10; i++ {        wg.Add(1)        go foo(ch, i)    }    wg.Wait()    close(ch)    for item := range ch {        fmt.Println(item)    }}当我像这样运行它时,它会打印fatal error: all goroutines are asleep - deadlock!我试图更改为缓冲通道,这解决了问题。但我真的很想知道为什么会有死锁。ch
查看完整描述

1 回答

?
慕码人8056858

TA贡献1803条经验 获得超6个赞

我已经注释掉了程序逻辑不正确的部分:


package main


import (

    "fmt"

    "sync"

)


var wg sync.WaitGroup


func foo(c chan int, i int) {

    defer wg.Done()

    c <- i

}


func main() {

    ch := make(chan int) // unbuffered channel


    for i := 0; i < 10; i++ {

        wg.Add(1)

        go foo(ch, i)

    }


    // wg.Wait is waiting for all goroutines to finish but that's

    // only possible if the send to channel succeeds. In this case,

    // it is not possible as your receiver "for item := range ch" is below

    // this. Hence, a deadlock.

    wg.Wait()


    // Ideally, it should be the sender's duty to close the channel.

    // And closing a channel before the receiver where the channel

    // is unbuffered is not correct.

    close(ch)


    for item := range ch {

        fmt.Println(item)

    }

}

更正的程序:


package main


import (

    "fmt"

    "sync"

)


var wg sync.WaitGroup


func foo(c chan int, i int) {

    defer wg.Done()

    c <- i

}


func main() {

    ch := make(chan int)


    go func() {

        for item := range ch {

            fmt.Println(item)

        }

    }()


    for i := 0; i < 10; i++ {

        wg.Add(1)

        go foo(ch, i)

    }


    wg.Wait()

    close(ch)

}


查看完整回答
反对 回复 2022-09-12
  • 1 回答
  • 0 关注
  • 61 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信