Go sync.WaitGroup 等待Goroutine执行完成

Posted vincenshen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Go sync.WaitGroup 等待Goroutine执行完成相关的知识,希望对你有一定的参考价值。

// 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.
//
// A WaitGroup must not be copied after first use.
type WaitGroup struct {
noCopy noCopy

// 64-bit value: high 32 bits are counter, low 32 bits are waiter count.
// 64-bit atomic operations require 64-bit alignment, but 32-bit
// compilers do not ensure it. So we allocate 12 bytes and then use
// the aligned 8 bytes in them as state.
state1 [12]byte
sema uint32
}

示例代码:

package main

import (
    "sync"
    "fmt"
)

func doWorker(id int, ch chan int, wg *sync.WaitGroup) {
    for n := range ch {
        fmt.Printf("Worker %d received %c
", id, n)
        wg.Done()    // 减少一个计数
    }
}

type worker struct {
    in chan int
    wg *sync.WaitGroup
}

func createWorker(id int, wg *sync.WaitGroup) worker {
    w := worker{
        in: make(chan int),
        wg: wg,
    }

    go doWorker(id, w.in, wg)
    return w
}

func chanDemo() {
    var wg sync.WaitGroup

    var workers [10]worker

    for i:=0; i<10; i++ {
        workers[i] = createWorker(i, &wg)
    }


    for i, worker := range workers {
        wg.Add(1)      // 添加一个计数
        worker.in <- a + i
    }
    wg.Wait()    // 阻塞,等待所有任务完成

}
func main() {
    chanDemo()
}

 

// A Mutex is a mutual exclusion lock.
// The zero value for a Mutex is an unlocked mutex.
//
// A Mutex must not be copied after first use.
type Mutex struct {
state int32
sema uint32
}

示例代码:

package main

import (
    "sync"
    "fmt"
)

var x = 0

func increment(wg *sync.WaitGroup, mu *sync.Mutex) {
    mu.Lock()
    x++
    mu.Unlock()
    wg.Done()
}

func main() {
    var wg sync.WaitGroup
    var mu sync.Mutex

    for i := 0; i < 1000; i++ {
        wg.Add(1)
        go increment(&wg, &mu)
    }
    wg.Wait()
    fmt.Println("final value of x", x)
}

 
























以上是关于Go sync.WaitGroup 等待Goroutine执行完成的主要内容,如果未能解决你的问题,请参考以下文章

go语言学习笔记 — 进阶 — 并发编程(11):同步sync,等待组(sync.WaitGroup)—— 保证在并发环境中完成指定数量的任务

Go 并发

Golang sync.WaitGroup 简介与用法

Golang sync.WaitGroup

Golang的sync.WaitGroup 实现逻辑和源码解析

WaitGroup