for循环导致死锁

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

for loop causing deadlock

问题

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

func gen(numbers ...int) <-chan int {
    out := make(chan int)

    go func() {
        for _, number := range numbers {
            out <- number
        }
        close(out)
    }()

    return out
}

func sq(in <-chan int) <-chan int {
    out := make(chan int)

    go func() {
        for number := range in {
            out <- number * number
        }
    }()

    return out
}

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

func main() {
    
    
    
    result := sq(sq(sq(gen(1, 2, 3, 4))))

    fmt.Println(<-result)
    fmt.Println(<-result)
    fmt.Println(<-result)
    fmt.Println(<-result)
    
    fmt.Println("-------------------")

    for channelValue := range sq(sq(sq(gen(1, 2, 3, 4)))) {
        fmt.Println(channelValue)
    }

}

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

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 :

func gen(numbers ...int) &lt;-chan int {
	out := make(chan int)

	go func() {
		for _, number := range numbers {
			out &lt;- number
		}
		close(out)
	}()

	return out
}

func sq(in &lt;-chan int) &lt;-chan int {
	out := make(chan int)

	go func() {
		for number := range in {
			out &lt;- number * number
		}
	}()

	return out
}

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

func main() {
	
	

	result := sq(sq(sq(gen(1, 2, 3, 4))))

	fmt.Println(&lt;-result)
	fmt.Println(&lt;-result)
	fmt.Println(&lt;-result)
	fmt.Println(&lt;-result)
    
    fmt.Println(&quot;-------------------&quot;)

	for channelValue := range sq(sq(sq(gen(1, 2, 3, 4)))) {
		fmt.Println(channelValue)
	}

}

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函数中没有关闭。

func sq(in <-chan int) <-chan int {
	out := make(chan int)

	go func() {
		for number := range in {
			out <- number * number
		}
		close(out)
	}()

	return out
}

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

英文:

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

func sq(in &lt;-chan int) &lt;-chan int {
  out := make(chan int)

  go func() {
	for number := range in {
		out &lt;- number * number
	}
	close(out)
  }()

  return out
}

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通道。

go func() {
	for number := range in {
		out <- number * number
	}
	close(out)
}()

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

英文:

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

go func() {
	for number := range in {
		out &lt;- number * number
	}
	close(out)
}()

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:

确定