douhu8851 2014-10-04 04:03
浏览 28
已采纳

以下golang代码死锁。 有人可以帮助理解为什么吗?

This example is taken from http://blog.golang.org/pipelines. It runs and gives correct answer but it shows following runtime error: "fatal error: all goroutines are asleep - deadlock!". Could anyone help me understand why this is happening? package main

import (
    "fmt"
)

func gen(nums ...int) <- chan int {
    out := make(chan int)
    go func() {
        for _, n := range nums {
            out <- n
        }
    }()
    return out
}

func sq(in <- chan int) <- chan int {
    out := make(chan int)
    go func() {
        for n := range in {
            out <- n * n
        }
        close(out)
    }()
    return out
}

func main() {
    for n := range sq(gen(2,3)) {
        fmt.Println(n)
    }
}

However the following modification doesn't.

func main() {
    // Set up the pipeline.
    c := gen(2, 3)
    out := sq(c)

    // Consume the output.
    fmt.Println(<-out) // 4
    fmt.Println(<-out) // 9
}
  • 写回答

1条回答 默认 最新

  • dpwu16132 2014-10-04 04:12
    关注

    The for n := range in of the sq() function never exits, and start blocking (after reading 2 values), because gen() never closed its channel.
    Adding close(out) to the go func of gen() would make it work: see playground.

    With channel, the receiver blocks until receiving a value.
    The range keyword, when used with a channel, will wait on the channel until it is closed.

    sq() is blocked, which means close(out) is never called, and in turn main() blocks on range sq() (since the channel sq isn't closed).

    In your second example, main() itself exits, which means even though sq() is blocked, everything still stops.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于github的项目怎么在pycharm上面运行
  • ¥15 内存地址视频流转RTMP
  • ¥100 有偿,谁有移远的EC200S固件和最新的Qflsh工具。
  • ¥15 找一个QT页面+目标识别(行人检测)的开源项目
  • ¥15 有没有整苹果智能分拣线上图像数据
  • ¥20 有没有人会这个东西的
  • ¥15 cfx考虑调整“enforce system memory limit”参数的设置
  • ¥30 航迹分离,航迹增强,误差分析
  • ¥15 Chrome Manifest扩展引用Ajax-hook库拦截请求失败
  • ¥15 用Ros中的Topic通讯方式控制小乌龟的速度,走矩形;编写订阅器代码