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

Barbara Streisand
Release: 2024-10-28 21:44:02
Original
296 people have browsed it

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

Deadlock Error in Go Channel

In Go, channels provide a means of communication between goroutines. However, improper use of channels can lead to deadlocks, where goroutines are blocked indefinitely.

One common cause of deadlocks with channels is when passing value types instead of pointers in goroutine functions. This is because Go passes value types by value, meaning that a copy of the value is created.

Consider this example:

<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>
Copy after login

Running the program results in a deadlock error:

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

The deadlock occurs because the WaitGroup is passed to the push and pull functions as a value. When the WaitGroup is updated in one of the goroutines, the changes are not reflected in the other goroutine because it has a copy of the value.

To resolve the deadlock, we need to pass the WaitGroup as a pointer, which ensures that both goroutines are operating on the same instance of the WaitGroup.

Here is the corrected version of the code:

<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>
Copy after login

The above is the detailed content of Why Does Passing Values Instead of Pointers Cause Deadlock in Go Channels?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!