2008-09-30 13:31:47 -06:00
|
|
|
// $G $D/$F.go && $L $F.$A && ./$A.out
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// make a lot of goroutines, threaded together.
|
|
|
|
// tear them down cleanly.
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2010-09-03 18:36:13 -06:00
|
|
|
"os"
|
|
|
|
"strconv"
|
2008-09-30 13:31:47 -06:00
|
|
|
)
|
|
|
|
|
2008-12-19 04:05:37 -07:00
|
|
|
func f(left, right chan int) {
|
2010-09-03 18:36:13 -06:00
|
|
|
left <- <-right
|
2008-09-30 13:31:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func main() {
|
2010-09-03 18:36:13 -06:00
|
|
|
var n = 10000
|
2009-05-08 16:21:41 -06:00
|
|
|
if len(os.Args) > 1 {
|
2011-11-01 20:06:05 -06:00
|
|
|
var err error
|
2010-09-03 18:36:13 -06:00
|
|
|
n, err = strconv.Atoi(os.Args[1])
|
2008-11-18 18:12:07 -07:00
|
|
|
if err != nil {
|
2010-09-03 18:36:13 -06:00
|
|
|
print("bad arg\n")
|
|
|
|
os.Exit(1)
|
2008-09-30 13:31:47 -06:00
|
|
|
}
|
|
|
|
}
|
2010-09-03 18:36:13 -06:00
|
|
|
leftmost := make(chan int)
|
|
|
|
right := leftmost
|
|
|
|
left := leftmost
|
2008-09-30 13:31:47 -06:00
|
|
|
for i := 0; i < n; i++ {
|
2010-09-03 18:36:13 -06:00
|
|
|
right = make(chan int)
|
|
|
|
go f(left, right)
|
|
|
|
left = right
|
2008-09-30 13:31:47 -06:00
|
|
|
}
|
2010-09-03 18:36:13 -06:00
|
|
|
go func(c chan int) { c <- 1 }(right)
|
|
|
|
<-leftmost
|
2008-09-30 13:31:47 -06:00
|
|
|
}
|