Home > Backend Development > Golang > ## How to Avoid Deadlocks in Concurrent Go Programs?

## How to Avoid Deadlocks in Concurrent Go Programs?

Susan Sarandon
Release: 2024-10-25 09:00:29
Original
782 people have browsed it

## How to Avoid Deadlocks in Concurrent Go Programs?

Solving Goroutines Deadlock in Go Concurrency

In concurrent Go programs, deadlocks can occur when goroutines indefinitely wait for each other to release resources. To resolve such deadlocks, consider the following example:

<code class="go">// Create a channel for communication between goroutines.
ch := make(chan int)

// Start producer goroutines that send values to the channel.
go producer(ch, 100*time.Millisecond, 2)
go producer(ch, 200*time.Millisecond, 5)

// Indefinite loop to continuously receive values from the channel.
for {
    fmt.Println(<-ch)
}</code>
Copy after login

This code results in a deadlock error: "fatal error: all goroutines are asleep - deadlock!" This occurs because the producers have a finite lifespan, eventually stopping transmission, while the consumer goroutine endlessly waits for new values. To avoid this deadlock, two main strategies can be employed:

1. Channel Termination:

Since channels can only be closed once, it's essential for producers to signal termination to the consumer. A coordinator can monitor the completion of producers and close the channel accordingly.

2. Coordinated Synchronization:

Using a synchronization primitive like sync.WaitGroup, producers can notify the coordinator when they have finished, and the coordinator can close the channel once all producers are done.

Updated Code Using Synchronization:

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

func producer(ch chan int, d time.Duration, num int, wg *sync.WaitGroup) {
    defer wg.Done()

    for i := 0; i < num; i++ {
        ch <- i
        time.Sleep(d)
    }
}

func main() {
    // Create a WaitGroup for coordinating producer completion.
    wg := &sync.WaitGroup{}

    // Initialize the channel for communication between goroutines.
    ch := make(chan int)

    // Start producer goroutines.
    wg.Add(2)
    go producer(ch, 100*time.Millisecond, 2, wg)
    go producer(ch, 200*time.Millisecond, 5, wg)

    // Assign a goroutine to close the channel when all producers have finished.
    go func() {
        wg.Wait()
        close(ch)
    }()

    // Iterate over values from the channel until it's closed.
    for v := range ch {
        fmt.Println(v)
    }
}</code>
Copy after login

Conclusion:

By implementing either channel termination or coordinated synchronization, developers can effectively avoid goroutines deadlocks and ensure proper coordination in concurrent Go programs.

The above is the detailed content of ## How to Avoid Deadlocks in Concurrent Go Programs?. 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