Home >Backend Development >Golang >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>
Running the program results in a deadlock error:
0 true 1 true 2 true 3 true 4 true throw: all goroutines are asleep - deadlock!
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>
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!