github.com/panjjo/go@v0.0.0-20161104043856-d62b31386338/misc/cgo/stdio/chain.go (about) 1 // cmpout 2 3 // Copyright 2009 The Go Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style 5 // license that can be found in the LICENSE file. 6 7 // +build test_run 8 9 // Pass numbers along a chain of threads. 10 11 package main 12 13 import ( 14 "runtime" 15 "strconv" 16 17 "../stdio" 18 ) 19 20 const N = 10 21 const R = 5 22 23 func link(left chan<- int, right <-chan int) { 24 // Keep the links in dedicated operating system 25 // threads, so that this program tests coordination 26 // between pthreads and not just goroutines. 27 runtime.LockOSThread() 28 for { 29 v := <-right 30 stdio.Stdout.WriteString(strconv.Itoa(v) + "\n") 31 left <- 1 + v 32 } 33 } 34 35 func main() { 36 leftmost := make(chan int) 37 var left chan int 38 right := leftmost 39 for i := 0; i < N; i++ { 40 left, right = right, make(chan int) 41 go link(left, right) 42 } 43 for i := 0; i < R; i++ { 44 right <- 0 45 x := <-leftmost 46 stdio.Stdout.WriteString(strconv.Itoa(x) + "\n") 47 } 48 }