首页  >  文章  >  后端开发  >  为什么传递值而不是指针会导致 Go Channel 死锁?

为什么传递值而不是指针会导致 Go Channel 死锁?

Barbara Streisand
Barbara Streisand原创
2024-10-28 21:44:02288浏览

Why Does Passing Values Instead of Pointers Cause Deadlock in Go Channels?

Go Channel 中的死锁错误

在 Go 中,通道提供了 goroutine 之间的一种通信方式。然而,通道使用不当可能会导致死锁,导致 goroutine 无限期阻塞。

通道死锁的一个常见原因是在 goroutine 函数中传递值类型而不是指针时。这是因为 Go 按值传递值类型,这意味着创建了值的副本。

考虑这个示例:

<code class="go">import (
    "fmt"
    "sync"
)

func push(c chan int, wg sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        c <- i
    }
    wg.Done()
}

func pull(c chan int, wg sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        result, ok := <-c
        fmt.Println(result, ok)
    }
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    c := make(chan int)

    go push(c, wg)
    go pull(c, wg)

    wg.Wait()
}</code>

运行程序会导致死锁错误:

    0 true
    1 true
    2 true
    3 true
    4 true
    throw: all goroutines are asleep - deadlock!

发生死锁是因为 WaitGroup 作为值传递给push 和 pull 函数。当其中一个 Goroutine 中的 WaitGroup 更新时,更改不会反映在另一个 Goroutine 中,因为它有值的副本。

要解决死锁,我们需要将 WaitGroup 作为指针传递,这确保两个 goroutine 都在 WaitGroup 的同一个实例上运行。

这是代码的正确版本:

<code class="go">import (
    "fmt"
    "sync"
)

func push(c chan int, wg *sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        c <- i
    }
    wg.Done()
}

func pull(c chan int, wg *sync.WaitGroup) {
    for i := 0; i < 5; i++ {
        result, ok := <-c
        fmt.Println(result, ok)
    }
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    wg.Add(2)
    c := make(chan int)

    go push(c, &wg)
    go pull(c, &wg)

    wg.Wait()
}</code>

以上是为什么传递值而不是指针会导致 Go Channel 死锁?的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn