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