竞态条件检测

disapear · · 2340 次点击
``` package main import ( "fmt" "sync" ) type test struct { channel chan chan int another chan int } func (t *test) consume() { for v := range t.another { select { case c := <-t.channel: c <- v case <-exit: break } } fmt.Println("finished") } var wg=sync.WaitGroup{} var exit =make(chan int,0) func main() { test := test{} test.channel = make(chan chan int, 10) test.another = make(chan int, 5) for i := 0; i < 10; i++ { wg.Add(1) go func(i int) { ch := make(chan int) ok: for { test.channel <- ch select { case v, ok := <-ch: if !ok { break } fmt.Println(v) case <-exit: break ok } } wg.Done() //fmt.Println("exit:",i) }(i) } go test.consume() for i := 0; i < 100; i++ { test.another <- i } close(test.another) close(exit) wg.Wait() test.channel = make(chan chan int, 100) } ```
#1