go routine deadlock with single channel

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

go routine deadlock with single channel

问题

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

  • 有多个工作协程
  • 项目可能同时在多个工作协程中找到
  • 一旦找到项目,所有工作协程都应该停止。
func workerRoutine(data Data, found chan bool, wg *sync.WaitGroup){
    defer (*wg).Done()
    // 数据处理
    // 返回false时退出

    // 多个协程可以同时设置这个值
    found <- true
}

func main() {
    // ....
    found := make(chan bool)
    var wg sync.WaitGroup
    itemFound := false
    Loop:
        for i := 0; i < limit; i++ {
            select {
            case <-found:
                itemFound = true
                break Loop
            default:
                if some_check {
                    wg.Add(1)
                    go workerRoutine(mdata, found, &wg)
                }
            }
        }

    wg.Wait()

    // 使用itemFound
}

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

英文:

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.

.

func workerRoutine(data Data, found chan bool, wg *sync.WaitGroup){

   defer (*wg).Done()
   // data processing
   // return on false 

   // multiple routines can set this at the same time
   found &lt;-true
}

func main {

   // ....
   found:=make(chan bool)
   var wg sync.WaitGroup
   itemFound:=false
       Loop:
          for i:=0; i&lt;limit; i++ {
              select {
                 case &lt;-found:
                    itemFound = true
                    break Loop
                 default:
                    if(some_check) {
                       wg.Add(1)
                       go workerRoutine(mdata,found,&amp;wg)
                    }
              }
       }

   wg.Wait()

   // use itemFound
}

答案1

得分: 1

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

示例代码:

package main
import "sync"

func worker(res chan bool, wg *sync.WaitGroup) {
    res <- true
    wg.Done()
}

func receiver(res chan bool, wg *sync.WaitGroup) {
    for range res {
    }
    wg.Done()
}

func main() {
    var wg, wg2 sync.WaitGroup
    wg.Add(1)
    wg2.Add(10)
    found := make(chan bool)
    go receiver(found, &wg)
    for i := 0; i < 10; i++ {
        go worker(found, &wg2)
    }
    wg2.Wait()
    close(found)
    wg.Done()
}

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

英文:

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

package main    
import &quot;sync&quot;

func worker(res chan bool, wg *sync.WaitGroup) {
	res &lt;- true
	wg.Done()
}

func receiver(res chan bool, wg *sync.WaitGroup) {
	for range res {
	}
	wg.Done()
}

func main() {
	var wg, wg2 sync.WaitGroup
	wg.Add(1)
	wg2.Add(10)
	found := make(chan bool)
	go receiver(found, &amp;wg)
	for i := 0; i &lt; 10; i++ {
		go worker(found, &amp;wg2)
	}
	wg2.Wait()
	close(found)
	wg.Done()
}

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:

确定