当前位置:网站首页>Go synchronization waiting group
Go synchronization waiting group
2022-06-25 02:40:00 【Pumpkin head 777】
Go In addition to using channels in language (channel) And mutex for synchronization between two concurrent programs , You can also use wait groups to synchronize multiple tasks , Waiting group can ensure that a specified number of tasks can be completed in a concurrent environment
stay sync.WaitGroup( Waiting group ) Type in the , Every sync.WaitGroup Value internally maintains a count , The initial default value for this count is zero .
package main
import (
"fmt"
"sync"
"time"
)
var wg sync.WaitGroup // Define a synchronization waiting group
func task(i int) {
fmt.Println("task...", i)
// Time consuming operational tasks , Network request , Read the file
time.Sleep(time.Second)
wg.Done() // Subtract a count
}
func main() {
for i := 0; i < 10; i++ {
wg.Add(1) // Add a count
go task(i)
}
wg.Wait() // Block until all tasks are completed
fmt.Println("over")
}
边栏推荐
- AI clothing generation helps you complete the last step of clothing design
- 折叠屏将成国产手机分食苹果市场的重要武器
- Distributed transaction solutions and code implementation
- Yarn: unable to load file c:\users\xxx\appdata\roaming\npm\yarn PS1 because running scripts is prohibited on this system
- ARM汇编中的栈桢小结
- 把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(4)—— 修改 oracle11g rac 集群的 scanIP
- Network planning | [four network layers] knowledge points and examples
- Is the compass reliable? Is it safe to open a securities account?
- 软件测试周刊(第77期):只要放弃一次,就会滋生放弃的习性, 原本可以解决的问题也会变得无法解决。
- Intranet learning notes (7)
猜你喜欢
![Network planning | [four network layers] knowledge points and examples](/img/c3/d7f382409e99eeee4dcf4f50f1a259.png)
Network planning | [four network layers] knowledge points and examples

Application of TSDB in civil aircraft industry

Pit entry machine learning: I. Introduction

Unity archive system - file in JSON format

高速缓存Cache详解(西电考研向)

Random list random generation of non repeating numbers

leecode学习笔记-机器人走到终点的最短路径

Use of hashcat

Sumati gamefi ecological overview, element design in the magical world
![Planification du réseau | [quatre couches de réseau] points de connaissance et exemples](/img/c3/d7f382409e99eeee4dcf4f50f1a259.png)
Planification du réseau | [quatre couches de réseau] points de connaissance et exemples
随机推荐
Jetson nano from introduction to practice (cases: opencv configuration, face detection, QR code detection)
Intranet learning notes (7)
PE文件基础结构梳理
PyTorch学习笔记(七)------------------ Vision Transformer
Intranet learning notes (6)
Please run IDA with elevated permissons for local debugging.
Summary of stack frame in arm assembly
Qt中使用QDomDocument操作XML文件
F - Spices(线性基)
Kaggle 专利匹配比赛金牌方案赛后总结
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
vie的刷新机制
MCN机构遍地开花:博主和作者要谨慎签约、行业水很深
【Proteus仿真】Arduino UNO+继电器控制照明设备
消息称一加将很快更新TWS耳塞、智能手表和手环产品线
When an interface has an exception, how do you analyze the exception?
[i.mx6ul] u-boot migration (VI) network driver modification lan8720a
Can automate - 10k, can automate - 20K, do you understand automated testing?
Detailed explanation of cache (for the postgraduate entrance examination of XD)
[analysis of STL source code] functions and applications of six STL components (directory)