2009-10-03 12:33:51 -06:00
|
|
|
// Copyright 2009 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
// Pass numbers along a chain of threads.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2009-12-15 16:33:31 -07:00
|
|
|
"runtime"
|
|
|
|
"stdio"
|
|
|
|
"strconv"
|
2009-10-03 12:33:51 -06:00
|
|
|
)
|
|
|
|
|
|
|
|
const N = 10
|
|
|
|
const R = 5
|
|
|
|
|
|
|
|
func link(left chan<- int, right <-chan int) {
|
|
|
|
// Keep the links in dedicated operating system
|
|
|
|
// threads, so that this program tests coordination
|
|
|
|
// between pthreads and not just goroutines.
|
2009-12-15 16:33:31 -07:00
|
|
|
runtime.LockOSThread()
|
2009-10-03 12:33:51 -06:00
|
|
|
for {
|
2009-12-15 16:33:31 -07:00
|
|
|
v := <-right
|
2010-07-14 18:17:53 -06:00
|
|
|
stdio.Stdout.WriteString(strconv.Itoa(v) + "\n")
|
2011-02-01 14:47:51 -07:00
|
|
|
left <- 1 + v
|
2009-10-03 12:33:51 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2009-12-15 16:33:31 -07:00
|
|
|
leftmost := make(chan int)
|
|
|
|
var left chan int
|
|
|
|
right := leftmost
|
2009-10-03 12:33:51 -06:00
|
|
|
for i := 0; i < N; i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
left, right = right, make(chan int)
|
|
|
|
go link(left, right)
|
2009-10-03 12:33:51 -06:00
|
|
|
}
|
|
|
|
for i := 0; i < R; i++ {
|
2009-12-15 16:33:31 -07:00
|
|
|
right <- 0
|
|
|
|
x := <-leftmost
|
2010-07-14 18:17:53 -06:00
|
|
|
stdio.Stdout.WriteString(strconv.Itoa(x) + "\n")
|
2009-10-03 12:33:51 -06:00
|
|
|
}
|
|
|
|
}
|