for循环导致死锁

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

for loop causing deadlock

问题

我已经从GO Concurrency中为你翻译了一些代码示例:

  1. func gen(numbers ...int) <-chan int {
  2. out := make(chan int)
  3. go func() {
  4. for _, number := range numbers {
  5. out <- number
  6. }
  7. close(out)
  8. }()
  9. return out
  10. }
  11. func sq(in <-chan int) <-chan int {
  12. out := make(chan int)
  13. go func() {
  14. for number := range in {
  15. out <- number * number
  16. }
  17. }()
  18. return out
  19. }

所以我尝试在我的主函数中使用上述代码,像这样:

  1. func main() {
  2. result := sq(sq(sq(gen(1, 2, 3, 4))))
  3. fmt.Println(<-result)
  4. fmt.Println(<-result)
  5. fmt.Println(<-result)
  6. fmt.Println(<-result)
  7. fmt.Println("-------------------")
  8. for channelValue := range sq(sq(sq(gen(1, 2, 3, 4)))) {
  9. fmt.Println(channelValue)
  10. }
  11. }

当我运行代码时,循环结束后我得到了这个错误信息:

fatal error: all goroutines are asleep - deadlock

请帮助我理解这个问题。根据我的理解,调用fmt.Println(result)四次与for channelValue := range sq(sq(sq(gen(1, 2, 3, 4))))的循环是相同的,这个理解正确吗?

请告诉我为什么在循环结束后我会遇到死锁问题?

英文:

I have written some code example from GO Concurrency :

  1. func gen(numbers ...int) &lt;-chan int {
  2. out := make(chan int)
  3. go func() {
  4. for _, number := range numbers {
  5. out &lt;- number
  6. }
  7. close(out)
  8. }()
  9. return out
  10. }
  11. func sq(in &lt;-chan int) &lt;-chan int {
  12. out := make(chan int)
  13. go func() {
  14. for number := range in {
  15. out &lt;- number * number
  16. }
  17. }()
  18. return out
  19. }

so I tried to use above code in my main function like this :

  1. func main() {
  2. result := sq(sq(sq(gen(1, 2, 3, 4))))
  3. fmt.Println(&lt;-result)
  4. fmt.Println(&lt;-result)
  5. fmt.Println(&lt;-result)
  6. fmt.Println(&lt;-result)
  7. fmt.Println(&quot;-------------------&quot;)
  8. for channelValue := range sq(sq(sq(gen(1, 2, 3, 4)))) {
  9. fmt.Println(channelValue)
  10. }
  11. }

I was confused when I run the code I got this message after the loop:

> fatal error: all goroutines are asleep - deadlock

Please help me to understand this. From what I understand is calling the fmt.Prinlnt(result) x 4 times is the same as the for loop on for channelValue := range sq(sq(sq(gen(1, 2, 3, 4)))). is this correct?

Could please tell me why I got deadlock after the loop?

答案1

得分: 2

通道阻塞的原因是通道在sq函数中没有关闭。

  1. func sq(in <-chan int) <-chan int {
  2. out := make(chan int)
  3. go func() {
  4. for number := range in {
  5. out <- number * number
  6. }
  7. close(out)
  8. }()
  9. return out
  10. }

解决这种死锁问题的一个好方法是向进程发送SIGQUIT信号。当接收到SIGQUIT信号时,运行时会转储所有goroutine的堆栈信息。堆栈转储通常会指出问题所在。

英文:

The range over the channel blocks because the channel is not closed in sq.

  1. func sq(in &lt;-chan int) &lt;-chan int {
  2. out := make(chan int)
  3. go func() {
  4. for number := range in {
  5. out &lt;- number * number
  6. }
  7. close(out)
  8. }()
  9. return out
  10. }

A good way to debug deadlocks like this is to send the process a SIGQUIT. The runtime dumps the stacks of all the goroutines when a SIGQUIT is received. The stack dumps will often point to the issue.

答案2

得分: 1

你没有在sq函数中关闭out通道。

  1. go func() {
  2. for number := range in {
  3. out <- number * number
  4. }
  5. close(out)
  6. }()

https://play.golang.org/p/kk8-08SfwB

英文:

You're not closing the out channel in the sq function

  1. go func() {
  2. for number := range in {
  3. out &lt;- number * number
  4. }
  5. close(out)
  6. }()

https://play.golang.org/p/kk8-08SfwB

huangapple
  • 本文由 发表于 2017年2月10日 06:58:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/42148500.html
匿名

发表评论

匿名网友

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

确定