What is the method set of `sync.WaitGroup`?

前端 未结 3 1335
[愿得一人]
[愿得一人] 2020-12-11 11:31

I have this simple program below

package main

import (
    \"fmt\"
    \"sync\"
    \"time\"
)

var wg sync.WaitGroup

func main() {
    wg.Add(1)

    go f         


        
3条回答
  •  不思量自难忘°
    2020-12-11 12:07

    From the documentation WaitGroup :

    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.

    From your Question

    How does this work?

    So for Add method is set the number of goroutine your called. From your code you have only one :

    func main() {
        wg.Add(1)
    
        go func() {
            fmt.Println("starting...")
            time.Sleep(1 * time.Second)
            fmt.Println("done....")
            wg.Done()
        } ()
    
        wg.Wait()
    
    }
    

    so it will tells goroutine to wait and print the result. as for wg.Done() this is for telling the one gouroutine has finished to work. and it tells to the add to decrement to -1. You can see the code below How Done method works :

    // Done decrements the WaitGroup counter.
    func (wg *WaitGroup) Done() {
        wg.Add(-1)
    }
    

    and finally for the Wait method this is for blocking goroutine until the WaitGroup counter is zero.

    And another:

    Why ?

    from your code above if you don't use WaitGroup you will not be able to print the result from the goroutine.

    All in all you can read it in the documentation.

提交回复
热议问题