小编Lon*_*ham的帖子

为什么我用这个Golang代码遇到死锁?

我对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, …
Run Code Online (Sandbox Code Playgroud)

deadlock go

4
推荐指数
1
解决办法
74
查看次数

标签 统计

deadlock ×1

go ×1