Home > Backend Development > Golang > How to Resolve Go Concurrency Deadlocks: 'all goroutines are asleep'?

How to Resolve Go Concurrency Deadlocks: 'all goroutines are asleep'?

Mary-Kate Olsen
Release: 2024-12-28 13:31:13
Original
857 people have browsed it

How to Resolve Go Concurrency Deadlocks:

Deadlock Error in Concurrency: "throw: All Goroutines Are Asleep"

When working with concurrency in Go, a deadlock can occur if all goroutines are waiting for each other to perform an action. One common error associated with this issue is "throw: all goroutines are asleep - deadlock!"

Understanding the Problem

Consider the following Go program:

package main

import (
    "fmt"
)

func total(ch chan int) {
    res := 0
    for iter := range ch {
        res += iter
    }
    ch <- res
}

func main() {
    ch := make(chan int)
    go total(ch)
    ch <- 1
    ch <- 2
    ch <- 3
    fmt.Println("Total is ", <-ch)
}
Copy after login

When running this program, we encounter the error "throw: all goroutines are asleep - deadlock!" The reason is that the range loop in the total function will never exit because we never close the ch channel. As a result, the goroutine waiting to receive the result in the main function will never receive it.

Resolving the Deadlock

To resolve this deadlock, we need to close the ch channel to indicate that no more values will be sent. Additionally, we can use a separate channel to send back the result, preventing a direct send and receive on the same channel.

The revised program below addresses these issues:

package main

import (
    "fmt"
)

func total(in chan int, out chan int) {
    res := 0
    for iter := range in {
        res += iter
    }
    out <- res // sends back the result
}

func main() {
    ch := make(chan int)
    rch := make(chan int)
    go total(ch, rch)
    ch <- 1
    ch <- 2
    ch <- 3
    close(ch) // this will end the loop in the total function
    result := <-rch // waits for total to give the result
    fmt.Println("Total is ", result)
}
Copy after login

By closing the ch channel and using a separate rch channel for the result, we eliminate the deadlock and allow the program to execute correctly.

The above is the detailed content of How to Resolve Go Concurrency Deadlocks: 'all goroutines are asleep'?. 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