golang:为什么这段代码没有死锁?

时间:2021-03-23 12:24:33

标签: go channels

golang:为什么这段代码没有死锁?

请通过以下代码:

package main
  

import (
    "fmt"
    "time"
)


func f1(done chan bool) {
    done <- true
    fmt.Printf("this's f1() goroutine\n")
}

func f2(done chan bool) {
    val := <-done
    fmt.Printf("this's f2() goroutine. val: %t\n", val)
}


func main() {
    done1 := make(chan bool)
    done2 := make(chan bool)

    go f1(done1)
    go f2(done2)
    
    fmt.Printf("main() go-routine is waiting to see deadlock.\n")
    time.Sleep(5 * time.Second)
}

如我们所见,go-routine f1() 正在向通道发送一个值。并且 go-routine f2() 正在从通道接收一个值。 但是,没有从 go-routine f1() 发送到的通道接收 go-routine。 类似地,go-routine f2() 正在接收的通道上没有 go-routine 发送。

3 个答案:

答案 0 :(得分:2)

正如@icza 的评论正确指出的那样,当所有 goroutines 被卡住并且无法取得进展时,就会发生死锁。在您的情况下,f1f2 卡住了,但主 goroutine 没有卡住 - 所以这不是死锁。

然而,它是一个goroutine泄漏!当某些代码完成其逻辑存在但让 goroutine 运行(未终止)时,就会发生 Goroutine 泄漏。我发现 github.com/fortytw2/leaktest 之类的工具可用于检测 goroutine 泄漏,它会检测您代码中的问题 - 试试看。

这是修改后的代码示例:

import (
    "testing"
    "time"

    "github.com/fortytw2/leaktest"
)


func f1(done chan bool) {
    done <- true
    fmt.Printf("this's f1() goroutine\n")
}

func f2(done chan bool) {
    val := <-done
    fmt.Printf("this's f2() goroutine. val: %t\n", val)
}

func TestTwoGoroutines(t *testing.T) {
    defer leaktest.CheckTimeout(t, time.Second)()

    done1 := make(chan bool)
    done2 := make(chan bool)

    go f1(done1)
    go f2(done2)
}

运行此测试时,您会看到类似以下内容:

--- FAIL: TestTwoGoroutines (1.03s)
    leaktest.go:132: leaktest: timed out checking goroutines
    leaktest.go:150: leaktest: leaked goroutine: goroutine 7 [chan send]:
        leaktest-samples.f1(0xc000016420)
            leaktest-samples/leaktest1_test.go:45 +0x37
        created by leaktest-samples.TestTwoGoroutines
            leaktest-samples/leaktest1_test.go:60 +0xd1
    leaktest.go:150: leaktest: leaked goroutine: goroutine 8 [chan receive]:
        leaktest-samples.f2(0xc000016480)
            leaktest-samples/leaktest1_test.go:50 +0x3e
        created by leaktest-samples.TestTwoGoroutines
            leaktest-samples/leaktest1_test.go:61 +0xf3

答案 1 :(得分:0)

正如@icza 所说,主协程可以继续并最终终止。

如果您从两个函数调用中的任何一个中删除 GET https://warm-wildwood-81069.herokuapp.com/socket.io/?EIO=4&transport=polling&t=NXV7rQn net::ERR_FAILED 关键字(使它们发生在主 goroutine 上),则应用程序将发生死锁。看看这个 go Playground(它为你突出了僵局)https://play.golang.org/p/r9qo2sc9LQA

答案 2 :(得分:0)

还有一件事要做。

func f1(done chan bool) {
    fmt.Printf("f1()\n")
}

func main() {
    done := make(chan bool)
    go f1(done)
    done <- true
}

在这里,调用者 go-routine 显然卡住了,因为不存在从通道接收的 go-routine。

在这里,并不是所有的 go-routine 都被阻塞了(f1() 漏过了),但仍然存在死锁。原因是,必须至少有 1 个 goroutine 应该从通道接收。 但这显然与上面的评论相矛盾,并不是所有的 go-routine 都被阻塞在这里。

相关问题