| 
									
										
										
										
											2009-01-26 11:34:38 -08: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. | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | package main | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-05-08 15:21:41 -07:00
										 |  |  | import "runtime" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07: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 11:34:38 -08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | func r(c chan int, m int) { | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 	for { | 
					
						
							| 
									
										
										
										
											2009-08-17 13:30:22 -07:00
										 |  |  | 		select { | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | 		case r := <-c: | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 			if h[r] != 1 { | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | 				println("r", | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 					"m=", m, | 
					
						
							|  |  |  | 					"r=", r, | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | 					"h=", h[r]) | 
					
						
							|  |  |  | 				panic("fail") | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 			} | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | 			h[r] = 2 | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2009-08-17 13:30:22 -07:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | func s(c chan int) { | 
					
						
							|  |  |  | 	for n := 0; n < N; n++ { | 
					
						
							|  |  |  | 		r := n | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 		if h[r] != 0 { | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | 			println("s") | 
					
						
							|  |  |  | 			panic("fail") | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | 		h[r] = 1 | 
					
						
							|  |  |  | 		c <- r | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | func main() { | 
					
						
							|  |  |  | 	c := make(chan int, W) | 
					
						
							|  |  |  | 	for m := 0; m < M; m++ { | 
					
						
							|  |  |  | 		go r(c, m) | 
					
						
							|  |  |  | 		runtime.Gosched() | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-03-24 16:46:53 -07:00
										 |  |  | 	runtime.Gosched() | 
					
						
							|  |  |  | 	runtime.Gosched() | 
					
						
							|  |  |  | 	s(c) | 
					
						
							| 
									
										
										
										
											2009-01-26 11:34:38 -08:00
										 |  |  | } |