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 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?