go routine deadlock with single channel

huangapple go评论110阅读模式
英文:

go routine deadlock with single channel

问题

我最近开始学习Go,并且在一个问题上遇到了困难。我有一个简单的Go协程,它要么返回值,要么将值推送到一个通道中。我的主函数将工作委托给这个协程,直到满足条件或数据用尽为止。这段代码似乎在"found"通道上发生了死锁。我做错了什么?

  • 有多个工作协程
  • 项目可能同时在多个工作协程中找到
  • 一旦找到项目,所有工作协程都应该停止。
  1. func workerRoutine(data Data, found chan bool, wg *sync.WaitGroup){
  2. defer (*wg).Done()
  3. // 数据处理
  4. // 返回false时退出
  5. // 多个协程可以同时设置这个值
  6. found <- true
  7. }
  8. func main() {
  9. // ....
  10. found := make(chan bool)
  11. var wg sync.WaitGroup
  12. itemFound := false
  13. Loop:
  14. for i := 0; i < limit; i++ {
  15. select {
  16. case <-found:
  17. itemFound = true
  18. break Loop
  19. default:
  20. if some_check {
  21. wg.Add(1)
  22. go workerRoutine(mdata, found, &wg)
  23. }
  24. }
  25. }
  26. wg.Wait()
  27. // 使用itemFound
  28. }

以上是你提供的代码。请问你需要我对其进行什么样的翻译?

英文:

I started learning go recently and I am stuck on a problem.
I have a simple go routine which either returns or pushes value to a channel.
And my main fn delegates work to this routine till it meets condition or data is exhausted.
This code seem to deadlock on "found" channel. What am I doing wrong?

  • There are multiple workers
  • Item can be found in more than one worker at the same time
  • Once item is found, all workers should be stopped.

.

  1. func workerRoutine(data Data, found chan bool, wg *sync.WaitGroup){
  2. defer (*wg).Done()
  3. // data processing
  4. // return on false
  5. // multiple routines can set this at the same time
  6. found &lt;-true
  7. }
  8. func main {
  9. // ....
  10. found:=make(chan bool)
  11. var wg sync.WaitGroup
  12. itemFound:=false
  13. Loop:
  14. for i:=0; i&lt;limit; i++ {
  15. select {
  16. case &lt;-found:
  17. itemFound = true
  18. break Loop
  19. default:
  20. if(some_check) {
  21. wg.Add(1)
  22. go workerRoutine(mdata,found,&amp;wg)
  23. }
  24. }
  25. }
  26. wg.Wait()
  27. // use itemFound
  28. }

答案1

得分: 1

一种可能的解决方案是避免使用select语句,并为接收器(或发送器,或两者)使用单独的goroutine。

示例代码:

  1. package main
  2. import "sync"
  3. func worker(res chan bool, wg *sync.WaitGroup) {
  4. res <- true
  5. wg.Done()
  6. }
  7. func receiver(res chan bool, wg *sync.WaitGroup) {
  8. for range res {
  9. }
  10. wg.Done()
  11. }
  12. func main() {
  13. var wg, wg2 sync.WaitGroup
  14. wg.Add(1)
  15. wg2.Add(10)
  16. found := make(chan bool)
  17. go receiver(found, &wg)
  18. for i := 0; i < 10; i++ {
  19. go worker(found, &wg2)
  20. }
  21. wg2.Wait()
  22. close(found)
  23. wg.Done()
  24. }

这段代码的作用是创建一个接收器和多个工作器的并发程序。接收器从通道found中接收数据,而工作器向通道found发送数据。通过使用sync.WaitGroup来同步goroutine的执行,确保所有工作器完成后再关闭通道。

英文:

One possible solution is to avoid select statement and use separate goroutine for receiver (or sender, or both).
Example:

  1. package main
  2. import &quot;sync&quot;
  3. func worker(res chan bool, wg *sync.WaitGroup) {
  4. res &lt;- true
  5. wg.Done()
  6. }
  7. func receiver(res chan bool, wg *sync.WaitGroup) {
  8. for range res {
  9. }
  10. wg.Done()
  11. }
  12. func main() {
  13. var wg, wg2 sync.WaitGroup
  14. wg.Add(1)
  15. wg2.Add(10)
  16. found := make(chan bool)
  17. go receiver(found, &amp;wg)
  18. for i := 0; i &lt; 10; i++ {
  19. go worker(found, &amp;wg2)
  20. }
  21. wg2.Wait()
  22. close(found)
  23. wg.Done()
  24. }

huangapple
  • 本文由 发表于 2015年10月9日 18:20:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/33035745.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定