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 Python语言实验
  • ¥15 SAP HANA SQL 增加合计行
  • ¥20 用C#语言解决一个英文打字练习器,有偿
  • ¥15 srs-sip外部服务 webrtc支持H265格式
  • ¥15 在使用abaqus软件中,继承到assembly里的surfaces怎么使用python批量调动
  • ¥15 大一C语言期末考试,求帮助🙏🙏
  • ¥15 ch340驱动未分配COM
  • ¥15 Converting circular structure to JSON
  • ¥30 Hyper-v虚拟机相关问题,求解答。
  • ¥15 TSM320F2808PZA芯片 Bootloader