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条)

报告相同问题?