所有的例程都睡着了 - 僵局

时间:2014-01-29 22:49:53

标签: concurrency go

我正在使用Go构建一个工人系统的骨架,我得到了“致命的错误:所有的goroutine都睡着了 - 死锁!”。

我使用两个通道进行协调,一个用于创建作业,第二个用于发送结果。创建作业后,我关闭了输入通道。

我的问题是如何关闭输出通道以便程序可以正确退出。 代码是:

package main

import (
    "bufio"
    "flag"
    "fmt"
    "log"
    "math/rand"
    "os"
    "time"
)

type Work struct {
    id int
    ts time.Duration
}

const (
    NumWorkers = 5000
    NumJobs    = 100000
)

func worker(in <-chan *Work, out chan<- *Work) {
    for w := range in {
        st := time.Now()
        time.Sleep(time.Duration(rand.Int63n(int64(200 * time.Millisecond))))
        w.ts = time.Since(st)
        out <- w
    }
}

func main() {
    wait := flag.Bool("w", false, "wait for <enter> before starting")
    flag.Parse()

    if *wait {
        fmt.Printf("I'm <%d>, press <enter> to continue", os.Getpid())
        reader := bufio.NewReader(os.Stdin)
        reader.ReadString('\n')
    }

    Run()
}

func Run() {
    in, out := make(chan *Work, 100), make(chan *Work, 100)
    for i := 0; i < NumWorkers; i++ {
        go worker(in, out)
    }
    go createJobs(in)
    receiveResults(out)
}

func createJobs(queue chan<- *Work) {
    for i := 0; i < NumJobs; i++ {
        work := &Work{i, 0}
        queue <- work
    }
    close(queue)
}

func receiveResults(completed <-chan *Work) {
    for w := range completed {
        log.Printf("job %d completed in %s", w.id, w.ts)
    }
}

感谢任何帮助:)

1 个答案:

答案 0 :(得分:1)

我错过了关于你在原始答案中知道死锁原因的部分。

  • 您提到WaitGroup,基本上只是一个信号量
  • 你可以使用另一个“控制”通道,工作人员在完成这些通道后,

-

func worker(ctrl chan<- bool, in <-chan *Work, out chan<- *Work) {
    for w := range in {
        st := time.Now()
        time.Sleep(time.Duration(rand.Int63n(int64(200 * time.Millisecond))))
        w.ts = time.Since(st)
        out <- w
    }
    ctrl <- true
}

func control(ctrl <-chan bool, numWorkers int, out chan<- *Work) {
    for i=0; i<numWorkers; i++ {
        <-ctrl
    }
    close(out)
}

原始答案:

您在完成后执行range

for w := range completed {
    log.Printf("job %d completed in %s", w.id, w.ts)
}

但该频道永不关闭