douzhou7124 2018-07-31 18:55
浏览 26
已采纳

同时计数树叶

There is a function that I want to write using a concurrency model in case the input is too large and parallel processing would be more efficient but it never ends.

Assuming there is a struct defined as:

type Tree struct {
    Name     string   `json:"name"`
    SubTrees []*Tree  `json:"subTrees,omitempty"`
    Leaves   []string `json:"leaves"`
}

I want to write a function that calculates the total number of Leaves throughout the entire recursive structure. This is easily done with recursion with:

func (tree *Tree) CountLeaves() int {
    curr := len(tree.Leaves)
    for _, s := range tree.SubTrees {
        curr += s.CountLeaves()
    }
    return curr
}

That's nice and all, but if the structure becomes too large, this is going to be inefficient so I wanted to refactor it to be concurrent and use channels. Here is my attempt at the refactor:

func (tree *Tree) CountLeaves() int {
    var wg sync.WaitGroup
    ch := make(chan int)
    defer close(ch)
    go count(tree, true, ch, &wg)

    var total int
    wg.Add(1)
    go func(total *int) {
        for x := range ch {
            fmt.Println(x)
            *total += x
        }
        wg.Done()
    }(&total)
    wg.Wait()

    return total
}

func count(t *Tree, root bool, ch chan int, wg *sync.WaitGroup) {
    defer wg.Done()
    ch <- len(t.Leaves)
    if t.SubTrees != nil {
        wg.Add(len(t.SubTrees))
        for _, s := range t.SubTrees {
            go count(s, false, ch, wg)
        }
        wg.Wait()
    }

    if root {
        ch <- -1
    }
}

I am able to currently gather all numbers through the channel that I would need to currently calculate the total number of Leaves but the function never ends. The terminating value -1 from the root Tree struct is never pushed or received through the channel and I can't figure out why.

Any ideas?

  • 写回答

1条回答 默认 最新

  • dqpc1845 2018-07-31 19:00
    关注

    I'm pretty sure your WaitGroup is just never getting enough wg.Done calls:

    go func(total *int) {
        for x := range ch {
            fmt.Println(x)
            *total += x
        }
        wg.Done()
    }(&total)
    

    Since you never close ch, the wg.Done will never be called here. I think if you move it inside the loop:

    go func(total *int) {
        for x := range ch {
            fmt.Println(x)
            *total += x
             wg.Done()
        }
    }(&total)
    

    That will resolve the issue.

    EDIT:

    Actually, I think there is one more issue:

    defer wg.Done()
    ch <- len(t.Leaves)
    if t.SubTrees != nil {
        wg.Add(len(t.SubTrees))
        for _, s := range t.SubTrees {
            go count(s, false, ch, wg)
        }
        wg.Wait()
    }
    

    The defered wg.Done() won't get called until you return, so this wg.Wait() will also wait forever. This hsould probable be:

    ch <- len(t.Leaves)
    if t.SubTrees != nil {
        wg.Add(len(t.SubTrees))
        for _, s := range t.SubTrees {
            go count(s, false, ch, wg)
        }
        wg.Done()
        wg.Wait()
    } else {
        wg.Done()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。