当前位置:网站首页>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")
}
边栏推荐
猜你喜欢

计网 | 【四 网络层】知识点及例题

random list随机生成不重复数

Sumati gamefi ecological overview, element design in the magical world

Leecode learning notes - the shortest path for a robot to reach its destination

软件测试人员的7个等级,据说只有1%的人能做到级别7
![[STL source code analysis] configurator (to be supplemented)](/img/87/0ed1895e9cdb5327411c0c9cb0197f.png)
[STL source code analysis] configurator (to be supplemented)

Intranet learning notes (5)

Random list random generation of non repeating numbers

Please run IDA with elevated permissons for local debugging.

折叠屏将成国产手机分食苹果市场的重要武器
随机推荐
Advanced usage of groovy
PSQL column to row
【STL源码剖析】STL六大组件功能与运用(目录)
把 Oracle 数据库从 Windows 系统迁移到 Linux Oracle Rac 集群环境(4)—— 修改 oracle11g rac 集群的 scanIP
ERROR日志格式与注意点
商城项目 pc----商品详情页
leecode学习笔记-机器人走到终点的最短路径
Yarn: unable to load file c:\users\xxx\appdata\roaming\npm\yarn PS1 because running scripts is prohibited on this system
华为、阿里等大厂程序员真的好找对象吗?
random list随机生成不重复数
背了八股文,六月赢麻了……
How to uninstall CUDA
It is said that Yijia will soon update the product line of TWS earplugs, smart watches and bracelets
Resolution of cross reference in IDA
Leecode learning notes - the shortest path for a robot to reach its destination
Dirvish Chinese document of vim
计算机三级(数据库)备考题目知识点总结
Migrate Oracle database from windows system to Linux Oracle RAC cluster environment (3) -- set the database to archive mode
Enlightenment of using shadergraph to make edge fusion particle shader
Application of TSDB in civil aircraft industry