关于go:双通道死锁

deadlock with double channel

我正在尝试创建一个程序(通过通道)将字符串发送到goroutine池。一旦goroutine完成工作,他们就会(通过其他渠道)发送一些结果。

代码为:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package main

import"fmt"
import"os"
import"sync"
import"bufio"

func worker(linkChan <-chan string, outChan chan<- string, wg *sync.WaitGroup, jobId int) {
   defer wg.Done()

   for url := range linkChan {
    // ...
     outChan <- url
   }
}

func main() {
    lCh := make(chan string)
    wg := new(sync.WaitGroup)
    outCh := make(chan string)

    urls := []string{}
    if len(os.Args) > 1 {
        for _, link := range os.Args[1:] {
            urls = append(urls, link)
        }
    } else {
        s := bufio.NewScanner(os.Stdin)
        for s.Scan() {
            urls = append(urls, s.Text())
        }
    }

    num_worker := 10

    for i := 0; i < num_worker; i++ {
        wg.Add(1)
        go worker(lCh, outCh, wg, i)
    }
    for _, link := range urls {
        lCh <- link
    }
    close(lCh)

    for res := range outCh {
        fmt.Printf("%s\
", res)
    }
    close(outCh)
    wg.Wait()

}

运行echo"something" | ./main会导致死锁。

据我了解,close(lCh)应该停止for url := range linkChan循环。我错了吗(由于代码死锁,所以看起来是这样)吗?

如何解决此僵局?

谢谢您的回答。


您需要在goroutine中泵送这些URL,否则outCh将被填充,因为您没有将其清空。这将使所有工人停滞不前,并且将陷入僵局。

因此重新排列代码,使其看起来像这样

1
2
3
4
5
6
7
8
9
10
11
12
13
go func() {
    for _, link := range urls {
        lCh <- link
    }
    close(lCh)
    wg.Wait()
    close(outCh)
}()

for res := range outCh {
    fmt.Printf("%s\
", res)
}

它将正常工作

完整代码


https://golang.org/ref/spec#For_range:

For channels, the iteration values produced are the successive values sent on the channel until the channel is closed. If the channel is nil, the range expression blocks forever.

在关闭outCh之前使用range。您必须在wg.Wait()之后关闭outCh,就像在Nick的答案中一样。