Source file test/ken/chan1.go

     1  // run
     2  
     3  // Copyright 2009 The Go Authors. All rights reserved.
     4  // Use of this source code is governed by a BSD-style
     5  // license that can be found in the LICENSE file.
     6  
     7  // Test communication with multiple simultaneous goroutines.
     8  
     9  package main
    10  
    11  import "runtime"
    12  
    13  const N = 1000 // sent messages
    14  const M = 10   // receiving goroutines
    15  const W = 2    // channel buffering
    16  var h [N]int   // marking of send/recv
    17  
    18  func r(c chan int, m int) {
    19  	for {
    20  		select {
    21  		case r := <-c:
    22  			if h[r] != 1 {
    23  				println("r",
    24  					"m=", m,
    25  					"r=", r,
    26  					"h=", h[r])
    27  				panic("fail")
    28  			}
    29  			h[r] = 2
    30  		}
    31  	}
    32  }
    33  
    34  func s(c chan int) {
    35  	for n := 0; n < N; n++ {
    36  		r := n
    37  		if h[r] != 0 {
    38  			println("s")
    39  			panic("fail")
    40  		}
    41  		h[r] = 1
    42  		c <- r
    43  	}
    44  }
    45  
    46  func main() {
    47  	c := make(chan int, W)
    48  	for m := 0; m < M; m++ {
    49  		go r(c, m)
    50  		runtime.Gosched()
    51  	}
    52  	runtime.Gosched()
    53  	runtime.Gosched()
    54  	s(c)
    55  }
    56  

View as plain text