2012-08-06 19:38:35 -06:00
|
|
|
// cmpout
|
|
|
|
|
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.
|
|
|
|
|
2012-03-06 21:27:30 -07:00
|
|
|
// +build ignore
|
|
|
|
|
2009-10-03 12:33:51 -06:00
|
|
|
// Pass numbers along a chain of threads.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2012-03-06 21:27:30 -07:00
|
|
|
"../stdio"
|
2009-12-15 16:33:31 -07:00
|
|
|
"runtime"
|
|
|
|
"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
|
|
|
}
|
|
|
|
}
|