公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

php小编百草在这篇文章中将解答一个常见问题:“使用 WaitGroups 和 Buffered Channels 的 Go 代码中出现死锁的原因是什么?”在Go语言中,WaitGroups和Buffered Channels是常用的并发编程工具。然而,有时候在使用它们的代码中可能会遇到死锁的情况。本文将深入探讨出现死锁的原因,并提供解决方案,帮助读者避免这种问题的发生。无论你是初学者还是有一定经验的Go开发者,本文都将为你提供有价值的信息。

问题内容

等待组、缓冲通道和死锁

我的这段代码会导致死锁,但我不确定为什么。我尝试在几个不同的地方使用互斥锁,关闭单独的 go 例程内外的通道,但结果仍然相同。

我尝试通过一个通道 (inputchan) 发送数据,然后从另一个通道 (outputchan) 读取数据

package main

import (
    "fmt"
    "sync"
)

func listStuff(wg *sync.WaitGroup, workerID int, inputChan chan int, outputChan chan int) {
    defer wg.Done()

    for i := range inputChan {
        fmt.Println("sending ", i)
        outputChan <- i
    }
}

func List(workers int) ([]int, error) {
    _output := make([]int, 0)

    inputChan := make(chan int, 1000)
    outputChan := make(chan int, 1000)

    var wg sync.WaitGroup
    wg.Add(workers)

    fmt.Printf("+++ Spinning up %v workers\n", workers)
    for i := 0; i < workers; i++ {
        go listStuff(&wg, i, inputChan, outputChan)
    }

    for i := 0; i < 3000; i++ {
        inputChan <- i
    }

    done := make(chan struct{})
    go func() {
        close(done)
        close(inputChan)
        close(outputChan)
        wg.Wait()
    }()

    for o := range outputChan {
        fmt.Println("reading from channel...")
        _output = append(_output, o)
    }

    <-done
    fmt.Printf("+++ output len: %v\n", len(_output))
    return _output, nil
}

func main() {
    List(5)
}

登录后复制

解决方法

主函数中的代码是连续的,首先尝试将 3k 值写入 inputchan 然后将从 outputchan 读取值。

您的代码会在第一个步骤中阻塞:

在 3k 值成功发送到 inputchan 之前,outputchan 不会流失任何内容,因此工作人员最终会在第一个 1k 值之后卡在 outputchan <- i
一旦工作人员停止从 inputchan 中消耗资源,main 将在大约 2k 个值之后卡在 inputchan <- i

解决此问题的一种方法是让生产者 (inputchan <- i) 和最终消费者 (for o := range outputchan {) 在单独的 goroutine 中运行。

您可以将这些演员之一保留在主 goroutine 中,并为另一个演员旋转一个新演员。例如:

go func(inputchan chan<- int){
    for i := 0; i < 3000; i++ {
        inputchan <- i
    }
    close(inputchan)
}(inputchan)

done := make(chan struct{})
go func() {
    close(done)
    // close(inputchan) // i chose to close inputchan above, don't close it twice
    close(outputchan)
    wg.wait()
}()

...

登录后复制

https://www.php.cn/link/80e4c54699b5b8cf8c67dd496909fceb

一个额外的注意事项:围绕发信号 done 的操作顺序很重要;通道 done 和 outputchan 只能在 wg.done() 指示所有工作人员完成后关闭

// it is best to close inputChan next to the code that controls
    // when its input is complete.
    close(inputChan)
    // If you had several producers writing to the same channel, you
    // would probably have to add a separate waitgroup to handle closing,
    // much like you did for your workers

    go func() {
        wg.Wait()
        // the two following actions must happen *after* workers have
        // completed
        close(done)
        close(outputChan)
    }()

登录后复制

分享到:
标签:Go语言
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定