dqq46733 2017-10-06 09:36
浏览 4
已采纳

卡在Golang的频道中

I need to run a function multiple times in parallel.
If even once the functions returns true (sends true on the channel) then the final result should be true.

How do I achieve this using goroutines and channels?

// Some performance intensive function
func foo(i int, c chan bool) {
    // do some processing and return either true or false
    c <- true // or false
}

func main() {
    flg := false
    ch := make(chan bool)
    for i := 0; i < 10; i++ {
        go foo(i, ch)
    }
    // If even once foo() returned true then val should be true
    flg = flg || <-ch
}
  • 写回答

2条回答 默认 最新

  • donglian2106 2017-10-06 11:12
    关注

    You only receive one value from the channel (which will be the value sent by one of the foo() calls, unpredictable which of the many), but you want to receive all.

    So use a for loop to receive as many values as you send (sent) on it:

    for i := 0; i < 10; i++ {
        flg = flg || <-ch
    }
    

    Although in your case it would be enough to loop until one true value is received, as that will determine the final value of flg, but it is still recommended to receive all values else the remaining goroutines will be blocked (as ch is an unbuffered channel). In this example it does not matter, but in a "real-life" application it would cause goroutines to stuck forever (memory-leak).

    If you don't want to wait for all foo() calls to complete and return as soon as possible (as soon as one true value is encountered), an option is to make ch buffered, so all goroutines can send values on it without getting blocked. And this way you are not required to receive (and thus wait for) all the foo() calls to complete:

    ch := make(chan bool, 10)
    for i := 0; i < 10; i++ {
        go foo(i, ch)
    }
    
    flg := false
    for i := 0; i < 10; i++ {
        if <-ch {
            flg = true
            break
        }
    }
    

    Choosing this approach, you should provide means to cancel goroutines whose work is no longer needed to avoid unnecessary CPU (and memory) usage. context.Context is such a mean, read more about it here: Close multiple goroutine if an error occurs in one in go.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程