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 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信
  • ¥15 改进Yolov8时添加的注意力模块在task.py里检测不到
  • ¥50 高维数据处理方法求指导
  • ¥100 数字取证课程 关于FAT文件系统的操作