私は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,
コードはエラーメッセージを返さずに適切に機能します。この動作をどのように説明できますか?ここで何か不足していますか?私が使用しているOSはWindows 10で、Golangのバージョンは1.8.3です。
事前にどうもありがとうございました。
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加