我在嘗試實(shí)現(xiàn)這個(gè)時(shí)遇到了一個(gè)問題(所有 goroutine 都睡著了 - 死鎖!)這是代碼的要點(diǎn):var workers = runtime.NumCPU()func main() { jobs := make(chan *myStruct, workers) done := make(chan *myStruct, workers) go produceWork(file_with_jobs, jobs) for i := 0; i < runtime.NumCPU(); i++ { go Worker(jobs, done) } consumeWork(done)}func produceWork(vf string, jobs chan *utils.DigSigEntries) { defer close(jobs) // load file with jobs file, err := ini.LoadFile(vf) // get data for processing for data, _ := range file { // ... jobs <- &myStruct{data1, data2, data3, false} }}func Worker(in, out chan *myStruct) { for { item, open := <-in if !open { break } process(item) out <- item } // close(out) --> tried closing the out channel, but then not all items are processed // though no panics occur.}func process(item *myStruct) { //...modify the item item.status = true}func consumeWork(done chan *myStruct) { for val := range done { if !val.status { fmt.Println(val) } }}我主要是想了解如何在不使用同步/等待的東西的情況下做到這一點(diǎn) - 只是純粹的渠道 - 這可能嗎?此例程的目標(biāo)是讓單個(gè)生產(chǎn)者加載由 N 個(gè)工人處理的項(xiàng)目 - 感謝任何指示/幫助。
- 2 回答
- 0 關(guān)注
- 331 瀏覽
添加回答
舉報(bào)
0/150
提交
取消