doubi9999 2018-03-27 09:40
浏览 33
已采纳

对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 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).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?