duanpi7578 2017-09-11 05:24
浏览 65
已采纳

如何根据该goroutine返回的值停止goroutine

Like here I created a go playground sample: sGgxEh40ev, but cannot get it work.

quit := make(chan bool)
res := make(chan int)

go func() {
    idx := 0
    for {
        select {
        case <-quit:
            fmt.Println("Detected quit signal!")
            return
        default:
            fmt.Println("goroutine is doing stuff..")
            res <- idx
            idx++
        }
    }

}()

for r := range res {
    if r == 6 {
        quit <- true
    }
    fmt.Println("I received: ", r)
}

Output:

goroutine is doing stuff..
goroutine is doing stuff..
I received:  0
I received:  1
goroutine is doing stuff..
goroutine is doing stuff..
I received:  2
I received:  3
goroutine is doing stuff..
goroutine is doing stuff..
I received:  4
I received:  5
goroutine is doing stuff..
goroutine is doing stuff..
fatal error: all goroutines are asleep - deadlock!

Is this possible? Where am I wrong

  • 写回答

3条回答 默认 最新

  • doucai1901 2017-09-11 06:51
    关注

    The problem is that in the goroutine you use a select to check if it should abort, but you use the default branch to do the work otherwise.

    The default branch is executed if no communications (listed in case branches) can proceed. So in each iteration quit channel is checked, but if it cannot be received from (no need to quit yet), default branch is executed, which unconditionally tries to send a value on res. Now if the main goroutine is not ready to receive from it, this will be a deadlock. And this is exactly what happens when the sent value is 6, because then the main goroutine tries to send a value on quit, but if the worker goroutine is in the default branch trying to send on res, then both goroutines try to send a value, and none is trying to receive! Both channels are unbuffered, so this is a deadlock.

    In the worker goroutine you must send the value on res using a proper case branch, and not in the default branch:

    select {
    case <-quit:
        fmt.Println("Detected quit signal!")
        return
    case res <- idx:
        fmt.Println("goroutine is doing stuff..")
        idx++
    }
    

    And in the main goroutine you must break out from the for loop so the main goroutine can end and so the program can end as well:

    if r == 6 {
        quit <- true
        break
    }
    

    Output this time (try it on the Go Playground):

    goroutine is doing stuff..
    I received:  0
    I received:  1
    goroutine is doing stuff..
    goroutine is doing stuff..
    I received:  2
    I received:  3
    goroutine is doing stuff..
    goroutine is doing stuff..
    I received:  4
    I received:  5
    goroutine is doing stuff..
    goroutine is doing stuff..
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 eda:门禁系统设计
  • ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
  • ¥15 376.1电表主站通信协议下发指令全被否认问题
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥15 复杂网络,变滞后传递熵,FDA
  • ¥20 csv格式数据集预处理及模型选择
  • ¥15 部分网页页面无法显示!
  • ¥15 怎样解决power bi 中设置管理聚合,详细信息表和详细信息列显示灰色,而不能选择相应的内容呢?
  • ¥15 QTOF MSE数据分析
  • ¥15 平板录音机录音问题解决