dongmengan8620 2015-08-24 01:38
浏览 4
已采纳

转到lang,频道处理顺序

I'm studying Go lang through 'A tour of Go', and it's hard to understand Go channel running sequence,

package main

import "fmt"
import "time"

func sum(a []int, c chan int) {

    sum := 0
    for _, v := range a {
        time.Sleep(1000 * time.Millisecond)
        sum += v
    }
    c <- sum // send sum to c
}

func main() {
    a := []int{7, 2, 8, -9, 4, 0}

    c := make(chan int)
    go sum(a[:len(a)/2], c)
    go sum(a[len(a)/2:], c)
    x, y := <-c, <-c // receive from c

    fmt.Println(x, y, x+y)
    fmt.Println("Print this first,")
}

If run above the code, I expected,

Print this first,
17 -5 12

Because, Go routine runs as non-blocking, a But, actually it prints,

17 -5 12
Print this first,

The other example that I found in internet,

package main

import "fmt"

type Data struct {
    i int
}

func func1(c chan *Data ) {
    fmt.Println("Called")
    for {
        var t *Data;
        t = <-c //receive
        t.i += 10 //increment
        c <- t   //send it back
    }
}

func main() {
    c := make(chan *Data)
    t := Data{10}
    go func1(c)
    println(t.i)
    c <- &t //send a pointer to our t
    i := <-c //receive the result
    println(i.i)
    println(t.i)
}

Also, I expected, it prints "Called" first, but the result is

10
20
20
Called

What I am misunderstanding? please help me to understand Go routine and channel.

  • 写回答

1条回答 默认 最新

  • dongpi0658 2015-08-24 02:19
    关注

    In your first example, x, y := <-c, <-c will block until it reads off c twice, and then assign values to x, y. Channels aside, you have an assignment, a print statement, then another print statement. Those are all synchronous things, and will happen in the order you state them in. There's no way the second print statement would print first.

    The second one is because fmt.Println writes to STDOUT and println to STDERR. If you are consistent (say use println everywhere) then you see:

    10
    Called
    20
    20
    

    That's cause there's a race between the first println(t.i) in the main and the println("Called") that's happening in the goroutine. I'm guessing with GOMAXPROCS set to 1, this will happen consistently. With GOMAXPROCS set to NumCPU, I get a mix of results, sometimes looking like the above, and sometimes like this:

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

报告相同问题?

悬赏问题

  • ¥15 mmocr的训练错误,结果全为0
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀