2012-02-16 21:50:37 -07:00
|
|
|
// run
|
2009-01-26 12:34:38 -07:00
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
2012-02-24 14:47:04 -07:00
|
|
|
// Test communication with multiple simultaneous goroutines.
|
2012-02-23 22:24:24 -07:00
|
|
|
|
2009-01-26 12:34:38 -07:00
|
|
|
package main
|
|
|
|
|
2009-05-08 16:21:41 -06:00
|
|
|
import "runtime"
|
|
|
|
|
2010-03-24 17:46:53 -06:00
|
|
|
const N = 1000 // sent messages
|
|
|
|
const M = 10 // receiving goroutines
|
|
|
|
const W = 2 // channel buffering
|
|
|
|
var h [N]int // marking of send/recv
|
2009-01-26 12:34:38 -07:00
|
|
|
|
2010-03-24 17:46:53 -06:00
|
|
|
func r(c chan int, m int) {
|
2009-01-26 12:34:38 -07:00
|
|
|
for {
|
2009-08-17 14:30:22 -06:00
|
|
|
select {
|
2010-03-24 17:46:53 -06:00
|
|
|
case r := <-c:
|
2009-01-26 12:34:38 -07:00
|
|
|
if h[r] != 1 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("r",
|
2009-01-26 12:34:38 -07:00
|
|
|
"m=", m,
|
|
|
|
"r=", r,
|
2010-03-24 17:46:53 -06:00
|
|
|
"h=", h[r])
|
|
|
|
panic("fail")
|
2009-01-26 12:34:38 -07:00
|
|
|
}
|
2010-03-24 17:46:53 -06:00
|
|
|
h[r] = 2
|
2009-01-26 12:34:38 -07:00
|
|
|
}
|
2009-08-17 14:30:22 -06:00
|
|
|
}
|
2009-01-26 12:34:38 -07:00
|
|
|
}
|
|
|
|
|
2010-03-24 17:46:53 -06:00
|
|
|
func s(c chan int) {
|
|
|
|
for n := 0; n < N; n++ {
|
|
|
|
r := n
|
2009-01-26 12:34:38 -07:00
|
|
|
if h[r] != 0 {
|
2010-03-24 17:46:53 -06:00
|
|
|
println("s")
|
|
|
|
panic("fail")
|
2009-01-26 12:34:38 -07:00
|
|
|
}
|
2010-03-24 17:46:53 -06:00
|
|
|
h[r] = 1
|
|
|
|
c <- r
|
2009-01-26 12:34:38 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-03-24 17:46:53 -06:00
|
|
|
func main() {
|
|
|
|
c := make(chan int, W)
|
|
|
|
for m := 0; m < M; m++ {
|
|
|
|
go r(c, m)
|
|
|
|
runtime.Gosched()
|
2009-01-26 12:34:38 -07:00
|
|
|
}
|
2010-03-24 17:46:53 -06:00
|
|
|
runtime.Gosched()
|
|
|
|
runtime.Gosched()
|
|
|
|
s(c)
|
2009-01-26 12:34:38 -07:00
|
|
|
}
|