douziqian2871 2015-03-22 02:35
浏览 60
已采纳

Golang Goroutine泄漏

// Copyright 2012 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import (
    "fmt"

    "code.google.com/p/go-tour/tree"
)

func walkImpl(t *tree.Tree, ch chan int) {
    if t == nil {
        return
    }
    walkImpl(t.Left, ch)
    ch <- t.Value
    walkImpl(t.Right, ch)
}

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch chan int) {
    walkImpl(t, ch)
    // Need to close the channel here
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
// NOTE: The implementation leaks goroutines when trees are different.
// See binarytrees_quit.go for a better solution.
func Same(t1, t2 *tree.Tree) bool {
    w1, w2 := make(chan int), make(chan int)

    go Walk(t1, w1)
    go Walk(t2, w2)

    for {
        v1, ok1 := <-w1
        v2, ok2 := <-w2
        if !ok1 || !ok2 {
            return ok1 == ok2
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Print("tree.New(1) == tree.New(1): ")
    if Same(tree.New(1), tree.New(1)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }

    fmt.Print("tree.New(1) != tree.New(2): ")
    if !Same(tree.New(1), tree.New(2)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }
}

In this code, a solution for http://tour.golang.org/concurrency/8

Why is there a comment on Same() func Same(t1, t2 *tree.Tree) bool saying that it leaks goroutines? How so? It also mentions a second file that fixes this:

// Copyright 2015 The Go Authors.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build ignore

package main

import (
    "fmt"

    "code.google.com/p/go-tour/tree"
)

func walkImpl(t *tree.Tree, ch, quit chan int) {
    if t == nil {
        return
    }
    walkImpl(t.Left, ch, quit)
    select {
    case ch <- t.Value:
        // Value successfully sent.
    case <-quit:
        return
    }
    walkImpl(t.Right, ch, quit)
}

// Walk walks the tree t sending all values
// from the tree to the channel ch.
func Walk(t *tree.Tree, ch, quit chan int) {
    walkImpl(t, ch, quit)
    close(ch)
}

// Same determines whether the trees
// t1 and t2 contain the same values.
func Same(t1, t2 *tree.Tree) bool {
    w1, w2 := make(chan int), make(chan int)
    quit := make(chan int)
    defer close(quit)

    go Walk(t1, w1, quit)
    go Walk(t2, w2, quit)

    for {
        v1, ok1 := <-w1
        v2, ok2 := <-w2
        if !ok1 || !ok2 {
            return ok1 == ok2
        }
        if v1 != v2 {
            return false
        }
    }
}

func main() {
    fmt.Print("tree.New(1) == tree.New(1): ")
    if Same(tree.New(1), tree.New(1)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }

    fmt.Print("tree.New(1) != tree.New(2): ")
    if !Same(tree.New(1), tree.New(2)) {
        fmt.Println("PASSED")
    } else {
        fmt.Println("FAILED")
    }
}

How does it accomplish that? Where was this leak? (to test the code you will have to run it on http://tour.golang.org/concurrency/8). Very confused and would appreciate some help, thanks!

  • 写回答

2条回答 默认 最新

  • dtr84664 2015-03-22 02:42
    关注

    The program stops receiving on the channels when a difference is detected.

    The walk goroutines run until they block sending to the channels. They never exit. This is the leak.

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

报告相同问题?

悬赏问题

  • ¥15 Matlab怎么求解含参的二重积分?
  • ¥15 苹果手机突然连不上wifi了?
  • ¥15 cgictest.cgi文件无法访问
  • ¥20 删除和修改功能无法调用
  • ¥15 kafka topic 所有分副本数修改
  • ¥15 小程序中fit格式等运动数据文件怎样实现可视化?(包含心率信息))
  • ¥15 如何利用mmdetection3d中的get_flops.py文件计算fcos3d方法的flops?
  • ¥40 串口调试助手打开串口后,keil5的代码就停止了
  • ¥15 电脑最近经常蓝屏,求大家看看哪的问题
  • ¥60 高价有偿求java辅导。工程量较大,价格你定,联系确定辅导后将采纳你的答案。希望能给出完整详细代码,并能解释回答我关于代码的疑问疑问,代码要求如下,联系我会发文档