当前位置:网站首页>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")
}
边栏推荐
- mysql命令备份
- 把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(3)—— 把数据库设置为归档模式
- 记一次beego通过go get命令后找不到bee.exe的坑
- [i.mx6ul] u-boot migration (VI) network driver modification lan8720a
- Talking about the advantages of flying book in development work | community essay solicitation
- Dirvish Chinese document of vim
- GO同步等待组
- 记一次beego通过go get命令后找不到bee.exe的坑
- Resolution of cross reference in IDA
- |How to analyze bugs? Professional summary and analysis
猜你喜欢

DDD concept is complex and difficult to understand. How to design code implementation model in practice?

【FPGA】串口以命令控制温度采集

使用ShaderGraph制作边缘融合粒子Shader的启示

华为、阿里等大厂程序员真的好找对象吗?

Lizuofan, co-founder of nonconvex: Taking quantification as his lifelong career

Experience of epidemic prevention and control, home office and online teaching | community essay solicitation
![[analysis of STL source code] functions and applications of six STL components (directory)](/img/f2/872fd93ef52b1424343ba634be24f6.png)
[analysis of STL source code] functions and applications of six STL components (directory)

1-6搭建Win7虚拟机环境

Detailed explanation of cache (for the postgraduate entrance examination of XD)

业务与技术双向结合构建银行数据安全管理体系
随机推荐
Once beego failed to find bee after passing the go get command Exe's pit
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (2) -- convert database to cluster mode
同花顺是正规平台吗?同花顺开户安全吗
入坑机器学习:一,绪论
random list随机生成不重复数
Squid 代理服务器之 ACL 访问控制
对进程内存的实践和思考
Mall project pc--- product details page
电脑端微信用户图片DAT格式解码为图片(TK版)
Can automate - 10k, can automate - 20K, do you understand automated testing?
商城项目 pc----商品详情页
vie的刷新机制
文件系统 -- 磁盘基础知识和FAT32文件系统详细介绍
Yarn: unable to load file c:\users\xxx\appdata\roaming\npm\yarn PS1 because running scripts is prohibited on this system
DDD concept is complex and difficult to understand. How to design code implementation model in practice?
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
Use of hashcat
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (1) -- migrate data to node 1
Software testing salary in first tier cities - are you dragging your feet
After reciting the eight part essay, I won the hemp in June