duancuan7057 2015-08-14 21:00
浏览 12

去选择频道并收集结果

Here's the code snippet:

for !done && outstanding > 0 {
  select {
  case signals := <- input:
    outstanding++
    go func() { results <- FFT(signals) }()
  case res <- results:
    outstanding--
    output <- results
  case <- ctx.Done():
    done = true
  }
}

In this video Richard Fliam says that:

In this code snippet we're farming out some work to multiple cores for FFTs. When cancellation is done on all outstanding results are collected and sent before the function returns. If we did not collect the results in this case we would have a memory leak. You can see that's because I'm not selecting against the Done context in that go func.

But when there are some outstanding results (the outstanding variable is greater than zero) and the operation is cancelled then the done variable is set to true and then the condition of the for loop would evaluate to false, so the outstanding results would not be collected.

Why does he say that the outstanding results are collected before the the function returns?

I'm new to go. Maybe I don't understand something fundamental concept.

Update:

IMO the outstanding results would always be collected if the condition of the for loop was the following:

// logical OR instead of AND
for !done || outstanding > 0 { ... }

Because when the operation is cancelled and there are still some outstanding results the condition of the for loop would evaluate to true. If all outstanding results arrive the for loop will end.

  • 写回答

1条回答 默认 最新

  • dsgdf45654 2015-08-14 21:28
    关注

    all results collected this line output <- results

    case res <- results:
        outstanding--
        output <- results
    

    this part of snippet is wrong, because then the double recieved chanel.

    Must be:

    case res <- results:
        outstanding--
        output <- res
    
    评论

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题