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