Getting “fatal error: all goroutines are asleep - deadlock!” when using sync.WaitGroup

后端 未结 2 1786
太阳男子
太阳男子 2021-01-05 14:46

I\'m trying to spin off a set of goroutines, and then wait for them all to finish.

import \"sync\"

func doWork(wg sync.WaitGroup) error {
    defer wg.Done(         


        
2条回答
  •  长情又很酷
    2021-01-05 15:13

    You need to pass a pointer to the WaitGroup, and not the WaitGroup object. When you pass the actual WaitGroup, Go makes a copy of the value, and calls Done() on the copy. The result is the original WaitGroup will have ten Add's and no Done's, and each copy of the WaitGroup will have one Done() and however many Add's were there when the WaitGroup was passed to the function.

    Pass a pointer instead, and every function will reference the same WaitGroup.

    import "sync"
    
    func doWork(wg *sync.WaitGroup) error {
        defer wg.Done()
        // Do some heavy lifting... request URL's or similar
        return nil
    }
    
    func main() {
        wg := &sync.WaitGroup{}
        for i := 0; i < 10; i++ {
            wg.Add(1)
            go doWork(wg)
        }
    }
    

提交回复
热议问题