dtvnbe1428 2018-05-18 09:25
浏览 9
已采纳

一个完成后如何完成所有goroutine

I want to run multiple goroutines, make some processing, put a result into channel, and when there's at least one goroutine finished, finish all others and return result from channel.

So, I tried to implement this using wait group, but seems I haven't use wait group properly.

    package optimizer

    import (
        "github.com/tevino/abool"
        "errors"
        "sync"
        "runtime"
        "log"
    )

    type Optimizer struct {
        Handlers      []ofdHandler.Handler
    }

    func Make(handlers []ofdHandler.Handler, maxProcs int) Optimizer {
        runtime.GOMAXPROCS(maxProcs)
        return Optimizer{Handlers: handlers}
    }

    func (o Optimizer) Optimize(params operations.GetV1ReceiptsParams) (*models.Receipt, error) {
        var wg sync.WaitGroup
        wg.Add(len(o.Handlers))

        results := make(chan *models.Receipt)
        isCalculated := abool.NewBool(false)

        for _, handler := range o.Handlers {
            go func(handler ofdHandler.Handler) {
                log.Println("Starting handler: ", handler.GetName())
                defer wg.Done()

                if isCalculated.IsSet() {
                    log.Println("Result is calculated, exiting goroutine...")
                    return
                }

                receipt, err := handler.Handle(params)
                if err != nil {
                    log.Println(err)
                    return
                }

                if isCalculated.IsSet() {
                    log.Println("Result is calculated, exiting goroutine...")
                    return
                }

                log.Println("Writing result to channel...")
                isCalculated.Set()
                results <- receipt
            }(handler)
        }

        log.Println("Waiting...")
        wg.Wait()

        if receipt, ok := <-results; ok {
            return receipt, nil
        }

        return nil, errors.New("couldn't optimize with current list of Handlers")
    }
  • 写回答

2条回答 默认 最新

  • doumi9661 2018-05-18 10:46
    关注

    The code you posted is not compilable as is and you don't describe the problem (what happens instead of expected result) either, but by looking at the code I have two suggestions:

    Make the result channel buffered, length 1:

    results := make(chan *models.Receipt, 1)
    

    This allows the goroutine which finishes first to write the result into the cannel and exit, thus decrementing the waitgroup's counter.

    In the end, instead of isCalculated.Set() you should use SetToIf so that you wouldn't set the flag/result twice (and thus stalling, as the reader is still waiting for the waitgroup to "complete" and so the second goroutine wouldn't be able to write into the channel, which means that the waitgroup never reaches to zero):

    log.Println("Writing result to channel...")
    if isCalculated.SetToIf(false, true) {
       results <- receipt
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了