How can I prevent deadlock when using WaitGroups and buffered channels in Go?

Linda Hamilton
Release: 2024-10-26 18:10:02
Original
308 people have browsed it

How can I prevent deadlock when using WaitGroups and buffered channels in Go?

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

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:

  1. Insufficient Channel Buffer: The channel has a capacity of 4, which is too small for the 5 goroutines trying to send data. When the channel becomes full, subsequent goroutines waiting to send data (line 15) will block indefinitely.
  2. Blocking Channel Iteration: The loop that iterates over the channel (lines 22-24) blocks indefinitely because it waits for more elements to arrive on the channel. Since all goroutines have finished sending data and no more data is expected, this iteration will never complete without a corresponding goroutine reading from the channel.

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

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

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!

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!