sync.WaitGroup
WaitGropu使用注意
作groutine参数时传指针
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, and the other 4 as storage
// for the sema.
state1 [3]uint32
}
WaitGroup 是结构体,传入使用值拷贝时,groutine内外是两个不同的WaitGroup,会造成逻辑混乱
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func(i int, wg *sync.WaitGroup) {
fmt.Println(i)
defer wg.Done()
}(i, &wg)
}
wg.Wait()
WaitGroup的Add要在goroutine前执行
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
go func(i int, wg *sync.WaitGroup) {
wg.Add(1)//应该在开始gorroutine之前调用wg.Add(1)来避免数据读写竞争
fmt.Println(i)
defer wg.Done()
}(i, &wg)
}
wg.Wait()
Add传入任意数字
var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1000)
go func(i int, wg *sync.WaitGroup) {
fmt.Println(i)
defer wg.Done()
}(i, &wg)
}
wg.Wait()
在使用上不会有什么问题,源码中根据输入的参数循环发送信号量;如果在高并发场景下会有性能问题;
WaitGroup的实现核心是 CAS使用
借用了CPU提供的原子性指令来实现。CAS操作修改共享变量时候不需要对共享变量加锁,而是通过类似乐观锁的方式进行检查,本质还是不断的占用CPU 资源换取加锁带来的开销(比如上下文切换开销)
sync.WaitGroup的更多相关文章
- Go并发控制之sync.WaitGroup
WaitGroup 会将main goroutine阻塞直到所有的goroutine运行结束,从而达到并发控制的目的.使用方法非常简单,真心佩服创造Golang的大师们! type WaitGroup ...
- sync—WaitGroup
用途:阻塞主线程的执行,直到所有的goroutine执行完成 WaitGroup总共有三个方法:Add(delta int),Done(),Wait().简单的说一下这三个方法的作用. Add:添加或 ...
- sync.WaitGroup和sync.Once
sync.WaitGroup,顾名思义,等待一组goroutinue运行完毕.sync.WaitGroup声明后即可使用,它有如下方法: func (wg *WaitGroup) Add(delta ...
- golang 的 sync.WaitGroup
WaitGroup的用途:它能够一直等到所有的goroutine执行完成,并且阻塞主线程的执行,直到所有的goroutine执行完成. 官方对它的说明如下: A WaitGroup waits for ...
- 《Go语言实战》笔记之协程同步 sync.WaitGroup
原文地址(欢迎互换友链): http://www.niu12.com/article/8 sync 包提供同步 goroutine 的功能 <p>文档介绍</p><cod ...
- golang-----golang sync.WaitGroup解决goroutine同步
go提供了sync包和channel来解决协程同步和通讯.新手对channel通道操作起来更容易产生死锁,如果时缓冲的channel还要考虑channel放入和取出数据的速率问题. 从字面就可以理解, ...
- golang sync.WaitGroup
//阻塞,直到WaitGroup中的所以过程完成. import ( "fmt" "sync" ) func wgProcess(wg *sync.WaitGr ...
- Golang sync.WaitGroup的用法
0x01 介绍 经常会看到以下了代码: 12345678910111213 package main import ( "fmt" "time") func m ...
- Golang的sync.WaitGroup 实现逻辑和源码解析
在Golang中,WaitGroup主要用来做go Routine的等待,当启动多个go程序,通过waitgroup可以等待所有go程序结束后再执行后面的代码逻辑,比如: func Main() { ...
- golang的sync.WaitGroup使用示例
下面一段代码 len(m) 不一定会打印为 10,为什么?.如果想要 len(m) 打印为 10,应该怎么修改代码? func main() { const N = 10 m := make(map[ ...
随机推荐
- NXOpen拉伸
#include <NXOpen/Annotations.hxx> #include <NXOpen/Assemblies_Component.hxx> #include &l ...
- Java_用数组保存并显示杨辉三角
import java.util.Scanner; public class Yang_Hui_Triangle { public static void main(String[] args) { ...
- Mysql 索引心得
1. 频繁查询的字段,应该创建索引. 2.更新非常频繁的字段,不应该创建索引. 3.唯一性太差的字段,比如 gender字段,就不应该创建索引. 4.不会出现在where条件之后的字段,不应该创建索引 ...
- 日志分析查看—— cat+grep+awk+uniq+sort+wc+join
有个统计日志信息的需求,下面是使用到的命令 //按 \t 对文件每一行进行切割,正则匹配第二列为32896时,输出第一列:再进行排序并去重,最后统计行数 cat file.log|awk -F '\t ...
- 活动 | Cloud Ace 受邀参加中国智造出海数字科技峰会
[Cloud Ace 是谷歌云全球战略合作伙伴,拥有 200 多名工程师,也是谷歌最高级别合作伙伴,多次获得 Google Cloud 合作伙伴奖. 作为谷歌托管服务商,我们提供谷歌云.谷歌地图.谷歌 ...
- laravel phpstorm ide-helper
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/ composer create-proj ...
- linux 软链接 硬链接 区别
来源 https://www.cnblogs.com/oceanftd/p/13475643.html 相关概念: 链接:简单说,链接就是一种文件共享的方式,是POSIX中的概念,主流文件系统都支持 ...
- SVN: E155004: THERE ARE UNFINISHED WORK ITEMS IN ''; RUN 'SVN CLEANUP' FIRST
eclipse的SVN更新或者还原都报错 使用clean up也不好用 解决办法 通过网址https://www.sqlite.org/download.html下载这个软件 解压放到.svn文件夹下 ...
- vagrant搭建centos7
准备工作 下载安装vagrant https://releases.hashicorp.com/vagrant/2.3.4/vagrant_2.3.4_windows_amd64.msi 选择自己需要 ...
- ubuntu usb network card drive
通过 lsusb -t命令查看网卡型号 /: Bus 02.Port 1: Dev 1, class="root_hub", Driver=xhci_hcd/4p, 5000M | ...