首页 文章

无缓冲的 Channels

提问于
浏览
1

我正在做Go Tour:http://tour.golang.org/#72这是我的代码:

package main

import (
    "code.google.com/p/go-tour/tree"
    "fmt"
)

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    var makeWalk func(t *tree.Tree, ch chan int)

    makeWalk = func(t *tree.Tree, ch chan int) {
        if t.Left != nil {
            makeWalk(t.Left, ch)

        }
        fmt.Println("-->", t.Value)
        ch <- t.Value
        fmt.Println("continue here")
        if t.Right != nil {
            makeWalk(t.Right, ch)
        }
    }
    makeWalk(t, ch)
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
 //   var ch_l chan int = make(chan int)
 // var ch_r chan int = make(chan int)
    return false
}

func main() {
    ch := make(chan int)
    go Walk(tree.New(1), ch)    
    for i := range(ch) {
        fmt.Println(i)
    }
}

这是输出:

--> 1
continue here
--> 2
1
2
continue here
--> 3
continue here
--> 4
3
4
continue here
--> 5
continue here
--> 6
5
6
continue here
--> 7
continue here
--> 8
7
8
continue here
--> 9
continue here
--> 10
9
10
continue here

据我所知,当它们传递值时,通道阻塞 . 我希望看到这样的输出:

--> 1
1
continue here
--> 2
2
continue here
...
--> 10
10
continue here

通道没有缓冲,是 fmt.Println 缓冲?这里发生了什么? :)

1 回答

  • 1

    当你提到 fmt.Println 时,你走在正确的轨道上 . 通道读取和写入不是调度程序可以切换到另一个goroutine的唯一时间 . 阻塞系统调用也可以触发上下文切换 .

    来自FAQ

    当协程阻塞时,例如通过调用阻塞系统调用,运行时自动将同一操作系统线程上的其他协同程序移动到另一个可运行的线程,这样它们就不会被阻塞 .

    fmt.Println 最终将调用阻塞系统调用( write() ),以便's why you'看到此行为 .

相关问题