Deadlock in Go: WaitGroup and Buffered Channels
In Go, deadlock occurs when concurrent goroutines wait indefinitely for each other to complete. One common cause of deadlock involves the use of WaitGroups and buffered channels.
Example of a Deadlock
Consider the following code:
<code class="go">package main import "fmt" import "sync" func main() { ch := make(chan []int, 4) var m []int var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { defer wg.Done() ch <- m // Sending to a full channel return }() } wg.Wait() for c := range ch { fmt.Printf("c is %v", c) } }</code>
This code intends to send 5 empty slices to a buffered channel with a capacity of 4 and then read from the channel after all goroutines complete. However, the code results in a deadlock error.
Cause of the Deadlock
The deadlock arises due to two issues:
Solution
To resolve the deadlock, make one of the following modifications:
Solution 1:
Increase the channel capacity to 5 (or more) and close it after sending all data:
<code class="go">ch := make(chan []int, 5) ... wg.Wait() close(ch)</code>
Solution 2:
Start a separate goroutine to read from the channel and notify the main goroutine when all data has been received:
<code class="go">func main() { ch := make(chan []int, 4) var m []int var wg sync.WaitGroup for i := 0; i < 5; i++ { wg.Add(1) go func() { ch <- m wg.Done() }() } go func() { for c := range ch { fmt.Printf("c is %v\n", c) wg.Done() } }() wg.Wait() }</code>
The above is the detailed content of How can I prevent deadlock when using WaitGroups and buffered channels in Go?. For more information, please follow other related articles on the PHP Chinese website!