Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can sync.WaitGroup safely be reused?

Tags:

go

Can sync.WaitGroup be reused after Wait() is called?

func worker(who string, in <-chan int, wg *sync.WaitGroup) {
    for i := range in {
        fmt.Println(who, i)
        wg.Done()
    }
}

func main() {
    var wg sync.WaitGroup

    AIn := make(chan int, 1)
    BIn := make(chan int, 1)

    go worker("a:", AIn, &wg)
    go worker("b:", BIn, &wg)

    for i := 0; i < 4; i++ {
        wg.Add(2)
        AIn <- i
        BIn <- i
        wg.Wait()
        fmt.Println("main:", i)
    }
}

This play.golang.org/p/QLsvA-b4Ae runs as expected, but is it guaranteed to be safe? The documentation doesn't say so, but maybe I'm just being paranoid.

like image 467
deft_code Avatar asked Aug 30 '13 01:08

deft_code


People also ask

Is WaitGroup a semaphore?

So from this, a sync. WaitGroup is a cross between a Barrier & a counting Semaphore (but without any concept of shared resource).

What is WaitGroup add?

A WaitGroup waits for a collection of goroutines to finish. The main goroutine calls Add to set the number of goroutines to wait for. Then each of the goroutines runs and calls Done when finished. At the same time, Wait can be used to block until all goroutines have finished.


1 Answers

Yes, it is safe. In fact, it is even safer than that. You can Wait from multiple goroutines concurrently, and interchange Add and Done calls as appropriate for your use case. As long as the Add happens before the Wait, you should be safe.

Just out of curiosity, right now the WaitGroup is implemented with a mutex, two int32s counters, and a semaphore:

type WaitGroup struct {
        m       Mutex
        counter int32
        waiters int32
        sema    *uint32
}

This is also an interesting test:

var wg1, wg2 sync.WaitGroup
wg1.Add(1)
wg1.Done()
wg1.Wait()
fmt.Println(wg1 == wg2) // Prints true

Finally, if you do find any issues with that kind of use, please report as it would be a bug.

like image 53
Gustavo Niemeyer Avatar answered Oct 02 '22 07:10

Gustavo Niemeyer