Home > Backend Development > Golang > Why Does My Go WaitGroup Cause a 'All Goroutines Are Asleep - Deadlock!' Error?

Why Does My Go WaitGroup Cause a 'All Goroutines Are Asleep - Deadlock!' Error?

Barbara Streisand
Release: 2024-12-25 19:14:17
Original
754 people have browsed it

Why Does My Go WaitGroup Cause a

WaitGroup Deadlock: Understanding "All Goroutines Are Asleep - Deadlock!"

When attempting to coordinate goroutines using a WaitGroup, it's possible to encounter the error "fatal error: all goroutines are asleep - deadlock!". This occurs when the goroutines responsible for decrementing the WaitGroup's counter inadvertently end up sleeping.

The Problem:

The following Go code illustrates how this error can arise:

package main

import (
    "fmt"
    "sync"
)

func main() {
    var wg sync.WaitGroup
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            fmt.Println("Hello", i)
        }()
    }
    wg.Wait()
}
Copy after login

Upon execution, this code should print "Hello" for each goroutine. However, instead, the program exits with the dreaded "deadlock" error.

The Reason:

The issue lies in the way the WaitGroup is passed to the goroutines. Go makes a copy of the value when it's passed to the anonymous function. So, each goroutine operates on a separate copy of the WaitGroup, none of which have their counter decremented, resulting in a perpetual sleep of the goroutines.

The Solution:

To resolve the deadlock, a pointer to the WaitGroup must be passed instead of the WaitGroup itself. Thus, all goroutines will reference the same WaitGroup, enabling them to decrement its counter correctly:

package main

import (
    "fmt"
    "sync"
)

func main() {
    wg := &sync.WaitGroup{}
    for i := 0; i < 10; i++ {
        wg.Add(1)
        go func() {
            defer wg.Done()
            fmt.Println("Hello", i)
        }()
    }
    wg.Wait()
}
Copy after login

By passing a pointer, the goroutines share the same WaitGroup object and can successfully coordinate their execution. This version of the code will correctly print "Hello" for each goroutine without triggering the deadlock.

The above is the detailed content of Why Does My Go WaitGroup Cause a 'All Goroutines Are Asleep - Deadlock!' Error?. 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