Home  >  Article  >  Backend Development  >  Golang concurrent programming advanced: in-depth understanding of WaitGroup

Golang concurrent programming advanced: in-depth understanding of WaitGroup

王林
王林Original
2023-09-27 08:36:211304browse

Golang concurrent programming advanced: in-depth understanding of WaitGroup

Golang concurrent programming advanced: in-depth understanding of WaitGroup

Introduction:
Concurrent programming is one of the most powerful features of Golang. When executing multiple tasks in parallel, a common scenario is to wait for all tasks to complete before proceeding to the next step. The sync package in Golang provides a good tool WaitGroup to solve this problem. This article will provide an in-depth introduction to the use of WaitGroup and give specific code examples.

1. What is WaitGroup?
WaitGroup is a structure in the sync package in Golang. It provides a simple and effective mechanism to wait for all goroutines to complete their tasks. WaitGroup maintains a counter internally. The value of the counter can be increased through the Add() method, the value of the counter can be decreased by the Done() method, and the Wait() method is used to block the current thread until the counter returns to zero.

2. Basic usage of WaitGroup
First, we need to import the sync package:

import "sync"

Then, we can follow the following steps to use WaitGroup:

  1. Create WaitGroup object:

    var wg sync.WaitGroup
  2. Use the Add() method to set the number of tasks to wait for:

    wg.Add(2)
  3. Start goroutine to execute tasks:

    go task1()
    go task2()
  4. Call the Done() method before the task ends to reduce the counter value:

    func task1() {
     defer wg.Done()
     // 执行task1的操作
    }
    
    func task2() {
     defer wg.Done()
     // 执行task2的操作
    }
  5. Finally, call it where you need to wait for all tasks to complete Wait() method:

    wg.Wait()

    In this way, the main thread will wait for all tasks to be completed before continuing to the next step.

3. Practical application examples of WaitGroup
Below we use a specific example to illustrate the usage of WaitGroup.

Suppose we have a requirement to download multiple network images concurrently and save them locally. When all images are downloaded, we need to perform subsequent processing. The code example is as follows:

package main

import (
    "fmt"
    "io"
    "net/http"
    "os"
    "sync"
)

var wg sync.WaitGroup

func main() {
    // 假设有3个图片需要下载
    imageUrls := []string{
        "https://example.com/image1.jpg",
        "https://example.com/image2.jpg",
        "https://example.com/image3.jpg",
    }

    // 设置需要等待的任务数量
    wg.Add(len(imageUrls))

    // 并发下载图片
    for _, imageUrl := range imageUrls {
        go downloadImage(imageUrl)
    }

    // 等待所有任务完成
    wg.Wait()

    fmt.Println("所有图片下载完成,进行后续处理")
}

func downloadImage(imageUrl string) {
    defer wg.Done()

    // 发送HTTP GET请求获取图片数据
    resp, err := http.Get(imageUrl)
    if err != nil {
        fmt.Printf("下载图片失败: %v
", err)
        return
    }
    defer resp.Body.Close()

    // 创建本地文件
    fileName := imageUrl[strings.LastIndex(imageUrl, "/")+1:]
    imgFile, err := os.Create(fileName)
    if err != nil {
        fmt.Printf("创建图片文件失败: %v
", err)
        return
    }
    defer imgFile.Close()

    // 将图片数据保存到本地文件
    _, err = io.Copy(imgFile, resp.Body)
    if err != nil {
        fmt.Printf("保存图片失败: %v
", err)
        return
    }

    fmt.Printf("图片下载成功: %v
", imageUrl)
}

In the above example code, we first define a package-level variable wg to manage the counter waiting for task completion. In the main function, we set the number of waiting tasks to the number of image URLs, and then start each image download task concurrently. After each task is completed, call the wg.Done() method to decrement the counter value. Finally, call the wg.Wait() method to wait for all tasks to complete. When all images are downloaded, continue to perform subsequent processing.

Summary:
This article introduces in detail the use of WaitGroup in Golang, and gives a specific example of multi-tasking concurrent downloading of images. By in-depth understanding of the use of WaitGroup, you can better master Golang's concurrent programming capabilities and improve program performance and efficiency. In practical applications, we can flexibly use WaitGroup to manage and wait for the completion of multiple goroutines according to specific needs.

The above is the detailed content of Golang concurrent programming advanced: in-depth understanding of WaitGroup. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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