doukao2180 2015-08-28 14:18
浏览 18
已采纳

Go中的频道/执行例程同步问题

Here is a small example program with the basic architecture/flow that I am trying to get working. How do I get all the numbers and "end" messages to print out? I have tried putting close statements here and there, but it either doesn't work, or I get panics about trying to close an already closed channel...

package main

import (
    "fmt"
    "time"
)

func main() {
    d := make(chan uint)

    go bar(d)

    c1 := make(chan uint)
    c2 := make(chan uint)
    c3 := make(chan uint)

    go foo(c1, d)
    go foo(c2, d)
    go foo(c3, d)

    c1 <- 1
    c2 <- 2
    c3 <- 3

    c1 <- 4
    c2 <- 5
    c3 <- 6

    c1 <- 7
    c2 <- 8
    c3 <- 9
}

func foo(c chan uint, d chan uint) {
    fmt.Println("foo start")

    for stuff := range c {
        time.Sleep(1)
        d <- stuff * 2
    }

    fmt.Println("foo end")
}

func bar(d chan uint) {
    fmt.Println("bar start")

    for stuff := range d {
        fmt.Printf("bar received %d
", stuff)
    }

    fmt.Println("bar end")
}

The output I am getting looks like this. Notice the last set of numbers and the "end" outputs are missing.

foo start
bar start
foo start
foo start
bar received 6
bar received 2
bar received 4
bar received 12
bar received 8
bar received 10

In my actual program, each "foo" function is doing filtering and a bunch of heavy string regexp stuff. And I need the "bar" function, because it has the job of reordering based on a timestamp, and serializing printing, so output doesn't get interlaced.

  • 写回答

2条回答 默认 最新

  • duannong1801 2015-08-28 14:31
    关注

    Your program is exiting before all goroutines are done. You need to wait for both the foo and bar goroutines to finish before returning from main.

    The usual way of doing this is by using a sync.WaitGroup, but since main isn't the producer for the d channel, you will have to ensure that all sends on that channel are finished before closing that with a second WaitGroup (or equivalent).

    var (
        fooWG sync.WaitGroup
        barWG sync.WaitGroup
    )
    
    func main() {
        d := make(chan uint)
    
        barWG.Add(1)
        go bar(d)
    
        c1 := make(chan uint)
        c2 := make(chan uint)
        c3 := make(chan uint)
    
        fooWG.Add(3)
        go foo(c1, d)
        go foo(c2, d)
        go foo(c3, d)
    
        c1 <- 1
        c2 <- 2
        c3 <- 3
    
        c1 <- 4
        c2 <- 5
        c3 <- 6
    
        c1 <- 7
        c2 <- 8
        c3 <- 9
    
        // close the channels so the foo goroutines can exit
        close(c1)
        close(c2)
        close(c3)
        fooWG.Wait()
    
        // all foo are done, so it's safe to close d and wait for bar
        close(d)
        barWG.Wait()
    }
    
    func foo(c chan uint, d chan uint) {
        defer fooWG.Done()
        fmt.Println("foo start")
    
        for stuff := range c {
            time.Sleep(1)
            d <- stuff * 2
        }
    
        fmt.Println("foo end")
    }
    
    func bar(d chan uint) {
        defer barWG.Done()
        fmt.Println("bar start")
    
        for stuff := range d {
            fmt.Printf("bar received %d
    ", stuff)
        }
    
        fmt.Println("bar end")
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格