dopr25398 2014-09-30 07:52
浏览 31
已采纳

转到频道:多行功能

I am learning Concurrency Pattern in Go, not sure about what is the purpose for Point A ?

Code is taken from: https://talks.golang.org/2012/concurrency.slide#30

Can anyone explain to me ? Thanks

type Message struct {
    str  string
    wait chan bool
}

func main() {
    c := fanIn(boring("Joe"), boring("Ann"))
    for i := 0; i < 5; i++ {
        msg1 := <-c
        fmt.Println(msg1.str)
        msg2 := <-c
        fmt.Println(msg2.str)
        msg1.wait <- true
        msg2.wait <- true
    }
    fmt.Println("You're both boring; I'm leaving.")
}

func fanIn(input1, input2 <-chan Message) <-chan Message {
    c := make(chan Message)
    go func() {
        for {
            c <- <-input1
        }
    }()
    go func() {
        for {
            c <- <-input2
        }
    }()
    return c
}

func boring(msg string) <-chan Message { 
    waitForIt := make(chan bool)
    c := make(chan Message)
    go func() { // We launch the goroutine from inside the function.
        for i := 0; ; i++ {
            c <- Message{fmt.Sprintf("%s %d", msg, i), waitForIt}
            time.Sleep(time.Duration(rand.Intn(2e3)) * time.Millisecond)
            <-waitForIt                          // <-----------------Point A
        }
    }()
    return c // Return the channel to the caller.
}
  • 写回答

3条回答 默认 最新

  • dongrao9454 2014-09-30 09:52
    关注

    fanIn spawns two goroutines reading from the first and second "boring" Message-channels. As either of both goroutine might be running (and the other one sleeping or running as well) we don't know the order in which the elements get written to the unifying channel:

    A A A A A \
                > A B B A A A B A B B
    B B B B B /
    

    Note that the resulting sequence has no particular order.

    This can be demonstrated by commenting out the "waiting"-channels:

    Joe 0
    Ann 0
    Joe 1
    Ann 1
    Joe 2
    Ann 2
    Joe 3
    Joe 4
    Joe 5
    Ann 3
    You're both boring; I'm leaving.
    

    The waiting channel exists to restore sequence as the slides you linked say. We want this:

    A A A A A \
                > A B A B A B A B A B
    B B B B B /
    

    Note how Ann and Joe talk one after the other now:

    Joe 0
    Ann 0
    Joe 1
    Ann 1
    Joe 2
    Ann 2
    Joe 3
    Ann 3
    Joe 4
    Ann 4
    You're both boring; I'm leaving.
    

    The author of this piece of code decided to synchronize the writes to the channels by letting them wait on waitForIt and notifying in main. This behavior relies on the first two messages we get in main's for-loop to be in order. If they are not we'd get the inverse sequence.

    Maybe I'm failing to see any more absolute synchronization guarantees, maybe the author didn't care about the order of the sequence as long as its a repeating sequence, but this code does not strike me as a particularly good example of concurrency and synchronization.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)