抛出:所有的goroutine都处于休眠状态 – 死锁

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

throw: all goroutines are asleep - deadlock

问题

给定以下简单的Go程序

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func total(ch chan int) {
  6. res := 0
  7. for iter := range ch {
  8. res += iter
  9. }
  10. ch <- res
  11. }
  12. func main() {
  13. ch := make(chan int)
  14. go total(ch)
  15. ch <- 1
  16. ch <- 2
  17. ch <- 3
  18. fmt.Println("Total is ", <-ch)
  19. }

我想知道为什么会出现以下错误信息

  1. throw: all goroutines are asleep - deadlock!

谢谢

英文:

Given the following simple Go program

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func total(ch chan int) {
  6. res := 0
  7. for iter := range ch {
  8. res += iter
  9. }
  10. ch &lt;- res
  11. }
  12. func main() {
  13. ch := make(chan int)
  14. go total(ch)
  15. ch &lt;- 1
  16. ch &lt;- 2
  17. ch &lt;- 3
  18. fmt.Println(&quot;Total is &quot;, &lt;-ch)
  19. }

I am wondering if someone can enlighten me as to why I get

  1. throw: all goroutines are asleep - deadlock!

thank you

答案1

得分: 34

由于您从未关闭ch通道,因此范围循环将永远不会结束。

您不能在同一个通道上发送结果。解决方案是使用另一个通道。

您的程序可以像这样进行调整:

  1. package main
  2. import (
  3. "fmt"
  4. )
  5. func total(in chan int, out chan int) {
  6. res := 0
  7. for iter := range in {
  8. res += iter
  9. }
  10. out <- res // 发送结果
  11. }
  12. func main() {
  13. ch := make(chan int)
  14. rch := make(chan int)
  15. go total(ch, rch)
  16. ch <- 1
  17. ch <- 2
  18. ch <- 3
  19. close (ch) // 这将结束total函数中的循环
  20. result := <- rch // 等待total给出结果
  21. fmt.Println("总和为", result)
  22. }
英文:

As you never close the ch channel, the range loop will never finish.

You can't send back the result on the same channel. A solution is to use a different one.

Your program could be adapted like this :

  1. package main
  2. import (
  3. &quot;fmt&quot;
  4. )
  5. func total(in chan int, out chan int) {
  6. res := 0
  7. for iter := range in {
  8. res += iter
  9. }
  10. out &lt;- res // sends back the result
  11. }
  12. func main() {
  13. ch := make(chan int)
  14. rch := make(chan int)
  15. go total(ch, rch)
  16. ch &lt;- 1
  17. ch &lt;- 2
  18. ch &lt;- 3
  19. close (ch) // this will end the loop in the total function
  20. result := &lt;- rch // waits for total to give the result
  21. fmt.Println(&quot;Total is &quot;, result)
  22. }

答案2

得分: -3

这也是正确的。

  1. package main
  2. import "fmt"
  3. func main() {
  4. c := make(chan int)
  5. go do(c)
  6. c <- 1
  7. c <- 2
  8. // close(c)
  9. fmt.Println("Total is ", <-c)
  10. }
  11. func do(c chan int) {
  12. res := 0
  13. // for v := range c {
  14. // res = res + v
  15. // }
  16. for i := 0; i < 2; i++ {
  17. res += <-c
  18. }
  19. c <- res
  20. fmt.Println("something")
  21. }
英文:

This is also right.

  1. package main
  2. import &quot;fmt&quot;
  3. func main() {
  4. c := make(chan int)
  5. go do(c)
  6. c &lt;- 1
  7. c &lt;- 2
  8. // close(c)
  9. fmt.Println(&quot;Total is &quot;, &lt;-c)
  10. }
  11. func do(c chan int) {
  12. res := 0
  13. // for v := range c {
  14. // res = res + v
  15. // }
  16. for i := 0; i &lt; 2; i++ {
  17. res += &lt;-c
  18. }
  19. c &lt;- res
  20. fmt.Println(&quot;something&quot;)
  21. }

huangapple
  • 本文由 发表于 2012年9月13日 09:51:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/12398359.html
匿名

发表评论

匿名网友

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

确定