duanjiao3754 2018-05-04 14:53
浏览 16
已采纳

永久循环读取两个通道的输出

I am working on the tree exercise of tour.golang. I have tried to implement the same function as written below.

func Same(t1, t2 *tree.Tree) bool {
    ch1 := make(chan int)
    ch2 := make(chan int)
    go Walk(t1, ch1);
    go Walk(t2, ch2);

    for c := range ch1 {
        d := <- ch2
        if c-d !=0 {
            return false
        }   

    }
    return true
}

Using the forever loop, I would like to compare if an output from ch1 is different from that of ch2. But the following is throwing this error:

fatal error: all goroutines are asleep - deadlock!

live version

  • 写回答

3条回答 默认 最新

  • dousong8187 2018-05-04 16:00
    关注

    You are seeing a deadlock for a very simple reason: you are ranging over ch1, but never closing it, so the for loop never terminates.

    You could fix this by manually iterating over each tree only a certain number of times like your 0..10 loop in main():

    // Same determines whether the trees
    // t1 and t2 contain the same values.
    func Same(t1, t2 *tree.Tree) bool {
        ch1 := make(chan int)
        ch2 := make(chan int)
        go Walk(t1, ch1)
        go Walk(t2, ch2)
    
        for i := 0; i < 10; i++ {
            c := <-ch1
            d := <-ch2
            if c-d != 0 {
                return false
            }
    
        }
        return true
    }
    

    Playground

    Alternatively, you can alter the signature of Walk to accept a waitgroup argument that is incremented by the caller of Walk and decremented when each Walk returns along with a goroutine to close the channel once you're done walking:

    // Walk walks the tree t sending all values
    // from the tree to the channel ch.
    func Walk(t *tree.Tree, ch chan int, wg *sync.WaitGroup) {
        defer wg.Done()
        if t.Left != nil {
            wg.Add(1)
            Walk(t.Left, ch, wg)
        }
        ch <- t.Value
        if t.Right != nil {
            wg.Add(1)
            Walk(t.Right, ch, wg)
        }
    
    }
    
    // Same determines whether the trees
    // t1 and t2 contain the same values.
    func Same(t1, t2 *tree.Tree) bool {
        ch1 := make(chan int)
        ch2 := make(chan int)
    
        var wg1 sync.WaitGroup
        wg1.Add(1)
        go Walk(t1, ch1, &wg1)
        go func() {
            wg1.Wait()
            close(ch1)
        }()
    
        var wg2 sync.WaitGroup
        wg2.Add(1)
        go Walk(t2, ch2, &wg2)
        go func() {
            // not strictly necessary, since we're not ranging over ch2, but here for completeness
            wg2.Wait()
            close(ch2)
        }()
    
        for c := range ch1 {
            d := <-ch2
            if c-d != 0 {
                return false
            }
    
        }
        return true
    }
    

    Playground

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

报告相同问题?

悬赏问题

  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化