2008-09-30 12:31:47 -07: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-04 10:36:13 +10:00
|
|
|
"os"
|
|
|
|
|
"strconv"
|
2008-09-30 12:31:47 -07:00
|
|
|
)
|
|
|
|
|
|
2008-12-19 03:05:37 -08:00
|
|
|
func f(left, right chan int) {
|
2010-09-04 10:36:13 +10:00
|
|
|
left <- <-right
|
2008-09-30 12:31:47 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func main() {
|
2010-09-04 10:36:13 +10:00
|
|
|
var n = 10000
|
2009-05-08 15:21:41 -07:00
|
|
|
if len(os.Args) > 1 {
|
2010-09-04 10:36:13 +10:00
|
|
|
var err os.Error
|
|
|
|
|
n, err = strconv.Atoi(os.Args[1])
|
2008-11-18 17:12:07 -08:00
|
|
|
if err != nil {
|
2010-09-04 10:36:13 +10:00
|
|
|
print("bad arg\n")
|
|
|
|
|
os.Exit(1)
|
2008-09-30 12:31:47 -07:00
|
|
|
}
|
|
|
|
|
}
|
2010-09-04 10:36:13 +10:00
|
|
|
leftmost := make(chan int)
|
|
|
|
|
right := leftmost
|
|
|
|
|
left := leftmost
|
2008-09-30 12:31:47 -07:00
|
|
|
for i := 0; i < n; i++ {
|
2010-09-04 10:36:13 +10:00
|
|
|
right = make(chan int)
|
|
|
|
|
go f(left, right)
|
|
|
|
|
left = right
|
2008-09-30 12:31:47 -07:00
|
|
|
}
|
2010-09-04 10:36:13 +10:00
|
|
|
go func(c chan int) { c <- 1 }(right)
|
|
|
|
|
<-leftmost
|
2008-09-30 12:31:47 -07:00
|
|
|
}
|