doubi9999
2018-03-27 09:40 阅读 30
已采纳

对Goroutines的通道参数感到困惑

I am learning about channels and concurrency in Go and I am confused on how the code below works.

package main

import (
    "fmt"
    "time"
    "sync/atomic"
)

var workerID int64
var publisherID int64

func main() {
    input := make(chan string)
    go workerProcess(input)
    go workerProcess(input)
    go workerProcess(input)
    go publisher(input)
    go publisher(input)
    go publisher(input)
    go publisher(input)
    time.Sleep(1 * time.Millisecond)
}

// publisher pushes data into a channel
func publisher(out chan string) {
    atomic.AddInt64(&publisherID, 1)
    thisID := atomic.LoadInt64(&publisherID)
    dataID := 0
    for {
        dataID++
        fmt.Printf("publisher %d is pushing data
", thisID)
        data := fmt.Sprintf("Data from publisher %d. Data %d", thisID, dataID)
        out <- data
    }
}

func workerProcess(in <-chan string) {
    atomic.AddInt64(&workerID, 1)
    thisID := atomic.LoadInt64(&workerID)
    for {
        fmt.Printf("%d: waiting for input...
", thisID)
        input := <-in
        fmt.Printf("%d: input is: %s
", thisID, input)
    }
}

This is what I understand please correct me if I'm wrong:

workerProcess Goroutine:

  1. Input channel taken as the argument which is assigned as the in channel.
  2. In the for loop, the first printf is executed.
  3. The value off the in channel is assigned to the variable input.
  4. The last printf is executed to show the value of thisID and input.(This doesn't really execute till after other Goroutines run).

publisher Goroutine:

  1. Input channel taken as the argument which is assigned as the out channel.
  2. In the for loop, dataID is incremented then first printf is executed.

  3. The string is assigned to data.

  4. The value of data is passed to out.

Questions:

  1. Where is the value from out getting passed to in the publisher Goroutine? It seems to be only in the scope of the for loop, wouldn't this cause deadlock. Since this is an unbuffered channel.

  2. I don't get how the workerProcess gets the data from publisher if all the Goroutines in main have the arguments as the channel input.

    I'm used to having code written like this if the output of one function is used in another:

    foo = fcn1(input) fcn2(foo)

I suspect it has something with how Goroutines run in the background but I'm not to sure I would appreciate an explanation.

  1. Why isn't the last printf statement in workerProcess not executed? My guess is the channel is empty, so it's waiting for a value.

Part of the output:

1: waiting for input...
publisher 1 is pushing data
publisher 1 is pushing data
1: input is: Data from publisher 1. Data 1
1: waiting for input...
1: input is: Data from publisher 1. Data 2
1: waiting for input...
publisher 1 is pushing data
publisher 1 is pushing data
publisher 2 is pushing data
2: waiting for input..
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

1条回答 默认 最新

  • 已采纳
    ds19891231 ds19891231 2018-03-27 10:01

    You have one channel, it is made with make in main.

    This one single channel is named in in workerProcess and out in publisher. out is a function argument to publisher which answers question 1.

    Question 2: That's the whole purpose of a channel. What you stuff into a channel on its input side comes out at its output side. If a function has reference to (one end of) such a channel it may communicate with someone else having reference to the same channel (its other end). What producer send to the channel is received by workerProcess. This send and receive is done through the special operator <- on Go. A fact you got slightly wrong in your explanation.

    out <- data takes data and sends it through the channel named out until <- in reads it from the channel (remember in and out name the same channel from main). That's how workerProcess and publisher communicate.

    Question 3 is a duplicate. Your whole program terminates once main is done (in your case after 1 millisecond. Nothing happens after termination of the program. Give the program more time to execute. (The non-printing is unrelated to the channel).

    点赞 评论 复制链接分享

相关推荐