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

预期输出以及工作池中的死锁

预期输出以及工作池中的死锁

Go
沧海一幻觉 2023-01-03 15:53:33
我正在学习 Go 并发并编写了强制性工作池示例,其中有 N 个工作和 M 个工作人员(N > M)。我遇到了一个死锁 ( all goroutines are asleep),我想不通;但是,我也在死锁发生之前得到了预期的输出,这让我更加困惑。有人可以指出我做错了什么吗?我的代码是这样的:package mainimport (    "fmt"    "sync")// A simple worker pool implementation using channels and WaitGroups.// Our workers simply read from a channel of integers from an input// channel and write their squares to an output channel.func addJobs(jobsCh chan<- int, wg *sync.WaitGroup) {    // 100 numbers to crunch (jobs)    for i := 1; i < 101; i++ {        jobsCh <- i    }    wg.Done()}func worker(jobsCh <-chan int, resultsCh chan<- int, wg2 *sync.WaitGroup) {    for num := range jobsCh {        resultsCh <- num * num    }    wg2.Done()}func addWorkers(jobsCh <-chan int, resultsCh chan<- int, wg *sync.WaitGroup) {    var wg2 sync.WaitGroup    // 10 workers    for i := 0; i < 10; i++ {        wg2.Add(1)        go worker(jobsCh, resultsCh, &wg2)    }    wg.Done()}func readResults(resultsCh <-chan int, wg *sync.WaitGroup) {    for sq := range resultsCh {        fmt.Printf("%v ", sq)    }    wg.Done()}func main() {    var wg sync.WaitGroup    jobsCh := make(chan int)    resultsCh := make(chan int)    wg.Add(1)    go addJobs(jobsCh, &wg)    wg.Add(1)    go addWorkers(jobsCh, resultsCh, &wg)    wg.Add(1)    go readResults(resultsCh, &wg)    wg.Wait()}正如预期的那样,这会打印数字的平方(以随机顺序),但也会遇到死锁。请参阅此游乐场链接。:(
查看完整描述

1 回答

?
九州编程

TA贡献1785条经验 获得超4个赞

关闭jobsCh导致工人退出:


func addJobs(jobsCh chan<- int, wg *sync.WaitGroup) {

    // 100 numbers to crunch (jobs)

    for i := 1; i < 101; i++ {

        jobsCh <- i

    }

    close(jobsCh)  // <-- add this line

    wg.Done()

}

工作人员完成后,关闭resultsCh导致结果循环退出:


func addWorkers(jobsCh <-chan int, resultsCh chan<- int, wg *sync.WaitGroup) {

    var wg2 sync.WaitGroup

    // 10 workers

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

        wg2.Add(1)

        go worker(jobsCh, resultsCh, &wg2)

    }

    wg2.Wait()       // <-- add this line

    close(resultsCh) // and this line

    wg.Done()

}


查看完整回答
反对 回复 2023-01-03
  • 1 回答
  • 0 关注
  • 71 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号