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条)

报告相同问题?

悬赏问题

  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题