如何在不发生死锁的情况下并行执行多个 goroutine?

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

How to execute multiple goroutines in parallel without a Deadlock

问题

所以我一直在尝试使用WaitGroup并行运行多个goroutine。无论我尝试什么,最终都会出现"fatal error: all goroutines are asleep - deadlock!"的错误。

这是我现在的代码:

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )
  7. func main() {
  8. ch := make(chan time.Duration)
  9. var wg sync.WaitGroup
  10. for _, v := range []time.Duration{5, 1} {
  11. wg.Add(1)
  12. go func() {
  13. defer wg.Done()
  14. wait(v, ch)
  15. }()
  16. }
  17. wg.Wait()
  18. }
  19. func wait(seconds time.Duration, c chan time.Duration) {
  20. time.Sleep(seconds * time.Second)
  21. c <- seconds
  22. }

然而,这导致了死锁,我无法弄清楚原因。

我一直在尝试使用以下代码在WaitGroup之后读取值:

  1. close(ch)
  2. for v := range ch {
  3. fmt.Println(v)
  4. }

然而,似乎它甚至无法达到这部分。

谢谢!

英文:

So I have been trying to run mutliple goroutines in parallel using a WaitGroup. Whatever I try I always end up with a "fatal error: all goroutines are asleep - deadlock!"

This is what my code looks like right now:

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sync&quot;
  5. &quot;time&quot;
  6. )
  7. func main() {
  8. ch := make(chan time.Duration)
  9. var wg sync.WaitGroup
  10. for _, v := range []time.Duration{5, 1} {
  11. wg.Add(1)
  12. go func() {
  13. defer wg.Done()
  14. wait(v, ch)
  15. }()
  16. }
  17. wg.Wait()
  18. }
  19. func wait(seconds time.Duration, c chan time.Duration) {
  20. time.Sleep(seconds * time.Second)
  21. c &lt;- seconds
  22. }

However this results in a deadlock and I can't figure out why.

I have been trying to read the values after the WaitGroup with the following code:

  1. close(ch)
  2. for v := range ch {
  3. fmt.Println(v)
  4. }

However it seems it wouldn't even reach this part.

Thank you!

答案1

得分: 1

在所有的工作线程完成后,你需要关闭工作通道。你现在是在主goroutine中立即关闭它。

所以你需要这样做:

  1. go func() {
  2. wg.Wait()
  3. close(ch)
  4. }()

另外,你的等待函数已经接受了一个time.Duration类型的参数,所以在那一点上它应该是真实的时间,而不是乘以time.Second。如果你想传入以秒为单位的值,考虑将输入类型更改为int,以避免混淆。

英文:

You need to close the worker channel after all the workers are done. You are closing it immediately from the main goroutine.

So do this:

  1. go func() {
  2. wg.Wait()
  3. close(ch)
  4. }()

Also your wait function already takes a time.Duration so really it should be in the real time at that point & not multiplied by time.Second. if you want to pass in units of seconds, consider changing the input type to int to avoid confusion.

答案2

得分: 1

代码存在两个问题:

  1. 必须在所有go协程完成后才能关闭通道。
  2. 在迭代过程中,v的值丢失了。

在Go语言中,for循环会重用v,因此所有的go协程在等待调用时都会有相同的值。

  1. package main
  2. import (
  3. "fmt"
  4. "sync"
  5. "time"
  6. )
  7. func main() {
  8. ch := make(chan time.Duration)
  9. var wg sync.WaitGroup
  10. for _, v := range []time.Duration{5, 1} {
  11. wg.Add(1)
  12. v := v // <- 这里
  13. go func() {
  14. defer wg.Done()
  15. wait(v, ch)
  16. }()
  17. }
  18. go func() { // 在go协程完成后关闭通道
  19. wg.Wait()
  20. close(ch)
  21. }()
  22. for v := range ch { // 循环直到通道关闭
  23. fmt.Println(v)
  24. }
  25. }
  26. func wait(seconds time.Duration, c chan time.Duration) {
  27. time.Sleep(seconds * time.Second)
  28. c <- seconds
  29. }
英文:

There are two problems withe the code:

  1. You must close the channel only after all go-routines have finished
  2. The value of v is being lost during the iteration

In go, the for loop will reuse v, so all go-routines will have the same value on the wait call.

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. &quot;sync&quot;
  5. &quot;time&quot;
  6. )
  7. func main() {
  8. ch := make(chan time.Duration)
  9. var wg sync.WaitGroup
  10. for _, v := range []time.Duration{5, 1} {
  11. wg.Add(1)
  12. v := v // &lt;- this
  13. go func() {
  14. defer wg.Done()
  15. wait(v, ch)
  16. }()
  17. }
  18. go func() { // Close the channel only after go-routines finish
  19. wg.Wait()
  20. close(ch)
  21. }()
  22. for v := range ch { // Will loop until channel is closed
  23. fmt.Println(v)
  24. }
  25. }
  26. func wait(seconds time.Duration, c chan time.Duration) {
  27. time.Sleep(seconds * time.Second)
  28. c &lt;- seconds
  29. }

huangapple
  • 本文由 发表于 2021年6月25日 03:49:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/68121993.html
匿名

发表评论

匿名网友

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

确定