英文:
Reusing a Go channel causes deadlock
问题
这段代码中发生了死锁。死锁发生在main
函数的第12行,也就是x, y := <-c, <-c
这一行。在这行代码中,程序试图从通道c
中接收两个值,但是在这个时刻,没有其他的goroutine在向通道c
发送值,因此程序无法继续执行,导致了死锁。要解决这个问题,你可以在main
函数中的go sendenum(0, c)
语句之后再添加一个相同的语句,以确保有足够的goroutine向通道发送值。
英文:
I'm new to golang
(whith Java
concurrency background). Consider this peace of code :
package main
import "fmt"
func sendenum(num int, c chan int) {
c <- num
}
func main() {
c := make(chan int)
go sendenum(0, c)
x, y := <-c, <-c
fmt.Println(x, y)
}
When I run this code , I get this error
fatal error: all goroutines are asleep - deadlock!
goroutine 1 [chan receive]:
main.main()
/home/tarrsalah/src/go/src/github.com/tarrsalah/stackoverflow/chan_dead_lock.go:12 +0x90
exit status 2
I know, adding another go sendenum(0, c)
statement fix the issue, ... but
When and Where the deadlock happened ?
答案1
得分: 5
在收到0之后,main
函数会继续等待在通道c
的接收端等待另一个值的到来(放入y
变量中),但是它永远不会到来,因为运行main
函数的goroutine是唯一剩下的活动goroutine。
当你添加另一个go sendenum(0, c)
时,它实际上在第二次通道接收时得到一个值,将其放入y
变量中,然后打印出x
和y
,程序成功结束。
英文:
After it receives the 0, main
keeps on waiting on the receiving end of c
for another value to arrive (to put in the y
variable), but it never will, as the goroutine running main
is the only one left to live.
When you add another go sendenum(0, c)
, it actually gets a value on the second channel receive, puts it into the y
variable, prints x
and y
out and the program finishes succesfully.
答案2
得分: 0
这不是“重复使用”通道的问题。只是因为代码规定了两次读取,但只有一次写入相同的通道,所以发生了简单的死锁。第二次读取永远不会发生,因此导致了死锁。
英文:
It's not that "reusing" a channel is a problem. It's just a simple deadlock happening because the code prescribes two reads, but only one write to the same channel. The second read can never happen, hence the deadlock.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论