缓存channel使用for-range?

bvaccc · · 1615 次点击
package main import ( "fmt" "runtime" ) func main() { runtime.GOMAXPROCS(runtime.NumCPU()) cs := make([]chan bool, 10) for i := 0; i < 10; i++ { cs[i] = make(chan bool, 1) go Go(cs[i], i) } for _, c := range cs { <-c } } func Go(c chan bool, index int) { a := 0 for i := 0; i < 10000000; i++ { a += i } fmt.Println(index, a) c <- true } 改成slice试试?
#2
更多评论
stevewang
耶和华是我的牧者,我必不至缺乏。
这里不适合用range,因为需要配合close,但是找不到合适的地方去close channel
#1
![D59AFC09-6D42-430E-A74F-7332FA089F92.png](http://studygolang.qiniudn.com/161223/d8e5dcb822871fe99a02f7d81a42f2c9.png)
#3