douju5062 2017-01-21 20:59
浏览 68

使用切片类型的输入和输出通道执行并发工作程序例程

I am relatively new to the Go language. Even though I don't hope so, I maybe bother you with a silly question. My apologies upfront, just in case...

Here's my example: I defined a worker() function which is called from main() as a set of concurrent Go routines. Input and output data is provided via an input and an output channel both of slice type []int. In one case everything works as expected, in the other case the result is faulty. See the comments in the code and the program output below the code.

Honestly, I don't see the actual difference between both code variants. What did I miss here? Thank you for any advice!

package main

import "fmt"
import "runtime"

func worker(x_ch <-chan []int, y_ch chan<- []int, wid int) {

    for x := range x_ch {
        y := x
        fmt.Println("   worker", wid, "x:", x)
        fmt.Println("   worker", wid, "y:", y)
        y_ch <- y
    }
}

func main() {

    n_workers := runtime.NumCPU()
    n_len := 4
    n_jobs := 4
    x := make([]int, n_len)
    x_ch := make(chan []int, 10)
    y_ch := make(chan []int, 10)

    for j := 0; j < n_workers; j++ { go worker(x_ch, y_ch, j) }

    for k := 0; k < n_jobs; k++ {

//      variant 1: works!
        x = []int{k, k, k, k}

//      variant 2: doesn't work!
//      for i := range x { x[i] = k }

        fmt.Println("main x:", k, x)
        x_ch <- x
    }

    close(x_ch)

    for i := 0; i < n_jobs; i++ {
        z := <- y_ch
        fmt.Println("       main y:", i, z)
    }
}

Correct output (variant 1):

main x: 0 [0 0 0 0]
main x: 1 [1 1 1 1]
main x: 2 [2 2 2 2]
main x: 3 [3 3 3 3]
   worker 3 x: [3 3 3 3]
   worker 3 y: [3 3 3 3]
   worker 2 x: [2 2 2 2]
   worker 2 y: [2 2 2 2]
   worker 1 x: [0 0 0 0]
   worker 1 y: [0 0 0 0]
   worker 0 x: [1 1 1 1]
   worker 0 y: [1 1 1 1]
       main y: 0 [3 3 3 3]
       main y: 1 [2 2 2 2]
       main y: 2 [0 0 0 0]
       main y: 3 [1 1 1 1]

Wrong output (variant 2):

main x: 0 [0 0 0 0]
main x: 1 [1 1 1 1]
main x: 2 [2 2 2 2]
main x: 3 [3 3 3 3]
   worker 3 x: [3 3 3 3]
   worker 3 y: [3 3 3 3]
       main y: 0 [3 3 3 3]
   worker 0 x: [2 2 2 2]
   worker 0 y: [3 3 3 3]
       main y: 1 [3 3 3 3]
   worker 1 x: [1 1 1 1]
   worker 1 y: [3 3 3 3]
       main y: 2 [3 3 3 3]
   worker 2 x: [3 3 3 3]
   worker 2 y: [3 3 3 3]
       main y: 3 [3 3 3 3]
  • 写回答

2条回答 默认 最新

  • dsideal2015 2017-01-21 22:24
    关注

    The difference is that in variant 1, you're sending a different slice every time, whereas in variant 2, you're sending the same slice every time (the one created above the for loops). Without creating a new slice, you're just setting the elements of the same slice to different values, so the goroutines see whatever values happen to be in the slice when they look at it. In variant 2, main will always see [3 3 3 3] because that's the final value after you've gone through the loop 4 times. The value of a slice object contains a reference to the underlying elements, not the elements themselves. There's a good explanation of slices here.

    评论

报告相同问题?

悬赏问题

  • ¥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,如何解決?