英文:
Why do I run into a deadlock with this Golang code?
问题
我对Golang还不太熟悉。你提供的代码是为了练习而写的,但遇到了一个死锁的运行时错误消息:
package main
import (
"fmt"
)
func system(WORKERS int) {
fromW := make(chan bool)
toW := make(chan bool)
for i := 0; i != WORKERS; i++ {
go worker(toW, fromW)
}
coordinator(WORKERS, fromW, toW)
}
func coordinator(WORKERS int, in, out chan bool) {
result := true
for i := 0; i != WORKERS; i++ {
result = result && <-in
}
for i := 0; i != WORKERS; i++ {
out <- result
}
fmt.Println("%t", result)
}
func worker(in, out chan bool) {
out <- false
<-in
}
func main() {
system(2)
}
然而,如果我将第19行的&&
操作符的操作数交换为:
result = <-in && result
那么代码将正常工作,不会返回任何错误消息。我该如何解释这种行为?我是否漏掉了什么?我使用的操作系统是Windows 10,Golang版本是1.8.3。
非常感谢您的帮助。
英文:
I am quite new to Golang. I wrote the following code for practice and ran into a run-time error message of a deadlock:
package main
import (
"fmt"
)
func system(WORKERS int) {
fromW := make(chan bool)
toW := make(chan bool)
for i := 0; i != WORKERS; i++ {
go worker(toW, fromW)
}
coordinator(WORKERS, fromW, toW)
}
func coordinator(WORKERS int, in, out chan bool) {
result := true
for i := 0; i != WORKERS; i++ {
result = result && <-in
}
for i := 0; i != WORKERS; i++ {
out <- result
}
fmt.Println("%t", result)
}
func worker(in, out chan bool) {
out <- false
<-in
}
func main() {
system(2)
}
However, if I swap the operands of && in line 19 to have
result = <-in && result,
the code works properly without returning any error messages. How can I explain this behaviour? Am I missing anything here? The OS I use is Windows 10, and the Golang version is 1.8.3.
Thank you very much in advance.
答案1
得分: 6
如您可以在这里看到,&&
的右操作数是有条件地进行评估的。
这意味着只有在 result
为真时,result = result && <-in
才会评估 <-in
。因此,协调器只从该通道读取一个 false
,并跳过从其他工作器读取消息的步骤。如果您交换 &&
的操作数位置,那么 <-in
将每次都会被评估,死锁问题就会消失。
英文:
As you can see here, the right operand of &&
is evaluated conditionally.
This means that result = result && <-in
will only evaluate <-in
if result
is true. So, the coodrinator reads only one false
from that channel, and skips reading messages from the other workers. If you switch the operands of &&
places, then the <-in
will evaluate every time and the deadlock goes away.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论