This post is part of a series about handling concurrency in Go:
- Go sync.Mutex: Normal and Starvation Mode
- Go sync.WaitGroup and The Alignment Problem (We're here)
- Go sync.Pool and the Mechanics Behind It
- Go sync.Cond, the Most Overlooked Sync Mechanism
- Go sync.Map: The Right Tool for the Right Job
- Go Singleflight Melts in Your Code, Not in Your DB
WaitGroup is basically a way to wait for several goroutines to finish their work.
Each of sync primitives has its own set of problems, and this one's no different. We're going to focus on the alignment issues with WaitGroup, which is why its internal structure has changed across different versions.
This article is based on Go 1.23. If anything changes down the line, feel free to let me know through X(@func25).
What is sync.WaitGroup?
If you're already familiar with sync.WaitGroup, feel free to skip ahead.
Let's dive into the problem first, imagine you've got a big job on your hands, so you decide to break it down into smaller tasks that can run simultaneously, without depending on each other.
To handle this, we use goroutines because they let these smaller tasks run concurrently:
func main() { for i := 0; i <p>But here's the thing, there's a <strong>good chance</strong> that the main goroutine finishes up and exits before the other goroutines are done with their work.</p> <p>When we're spinning off many goroutines to do their thing, we want to keep track of them so that the main goroutine doesn't just finish up and exit before everyone else is done. That's where the WaitGroup comes in. Each time one of our goroutines wraps up its task, it lets the WaitGroup know. </p> <p>Once all the goroutines have checked in as ‘done,' the main goroutine knows it's safe to finish, and everything wraps up neatly.<br> </p> <pre class="brush:php;toolbar:false">func main() { var wg sync.WaitGroup wg.Add(10) for i := 0; i <p>So, here's how it typically goes:</p>
- Adding goroutines: Before starting your goroutines, you tell the WaitGroup how many to expect. You do this with WaitGroup.Add(n), where n is the number of goroutines you're planning to run.
- Goroutines running: Each goroutine goes off and does its thing. When it's done, it should let the WaitGroup know by calling WaitGroup.Done() to reduce the counter by one.
- Waiting for all goroutines: In the main goroutine, the one not doing the heavy lifting, you call WaitGroup.Wait(). This pauses the main goroutine until that counter in the WaitGroup reaches zero. In plain terms, it waits until all the other goroutines have finished and signaled they're done.
Usually, you'll see WaitGroup.Add(1) being used when firing up a goroutine:
for i := 0; i <p>Both ways are technically fine, but using wg.Add(1) has a small performance hit. Still, it's less error-prone compared to using wg.Add(n).</p> <blockquote> <p><em>"Why is wg.Add(n) considered error-prone?"</em></p> </blockquote> <p>The point is this, if the logic of the loop changes down the road, like if someone adds a continue statement that skips certain iterations, things can get messy:<br> </p> <pre class="brush:php;toolbar:false">wg.Add(10) for i := 0; i <p>In this example, we're using wg.Add(n) before the loop, assuming the loop will always start exactly n goroutines. </p> <p>But if that assumption doesn't hold, like if some iterations get skipped, your program might get stuck waiting for goroutines that were never started. And let's be honest, that's the kind of bug that can be a real pain to track down.</p> <p>In this case, wg.Add(1) is more suitable. It might come with a tiny bit of performance overhead, but it's a lot better than dealing with the human error overhead.</p> <p>There's also a pretty common mistake people make when using sync.WaitGroup:<br> </p> <pre class="brush:php;toolbar:false">for i := 0; i <p>Here's what it comes down to, wg.Add(1) is being called <strong>inside</strong> the goroutine. This can be an issue because the goroutine might start running after the main goroutine has already called wg.Wait(). </p> <p>That can cause all sorts of timing problems. Also, if you notice, all the examples above use defer with wg.Done(). It indeed should be used with defer to avoid issues with multiple return paths or panic recovery, making sure that it always gets called and doesn't block the caller indefinitely.</p> <p>That should cover all the basics.</p> <h2> How sync.WaitGroup Looks Like? </h2> <p>Let's start by checking out the source code of sync.WaitGroup. You'll notice a similar pattern in sync.Mutex.</p> <blockquote> <p>Again, if you're not familiar with how a mutex works, I strongly suggest you check out this article first: Go Sync Mutex: Normal & Starvation Mode.<br> </p> </blockquote> <pre class="brush:php;toolbar:false">type WaitGroup struct { noCopy noCopy state atomic.Uint64 sema uint32 } type noCopy struct{} func (*noCopy) Lock() {} func (*noCopy) Unlock() {}
In Go, it's easy to copy a struct by just assigning it to another variable. But some structs, like WaitGroup, really shouldn't be copied.
Copying a WaitGroup can mess things up because the internal state that tracks the goroutines and their synchronization can get out of sync between the copies. If you've read the mutex post, you'll get the idea, imagine what could go wrong if we copied the internal state of a mutex.
The same kind of issues can happen with WaitGroup.
noCopy
The noCopy struct is included in WaitGroup as a way to help prevent copying mistakes, not by throwing errors, but by serving as a warning. It was contributed by Aliaksandr Valialkin, CTO of VictoriaMetrics, and was introduced in change #22015.
The noCopy struct doesn't actually affect how your program runs. Instead, it acts as a marker that tools like go vet can pick up on to detect when a struct has been copied in a way that it shouldn't be.
type noCopy struct{} func (*noCopy) Lock() {} func (*noCopy) Unlock() {}
Its structure is super simple:
- It has no fields, so it doesn't take up any meaningful space in memory.
- It has two methods, Lock and Unlock, which do nothing (no-op). These methods are there just to work with the -copylocks checker in the go vet tool.
When you run go vet on your code, it checks to see if any structs with a noCopy field, like WaitGroup, have been copied in a way that could cause issues.
It will throw an error to let you know there might be a problem. This gives you a heads-up to fix it before it turns into a bug:
func main() { var a sync.WaitGroup b := a fmt.Println(a, b) } // go vet: // assignment copies lock value to b: sync.WaitGroup contains sync.noCopy // call of fmt.Println copies lock value: sync.WaitGroup contains sync.noCopy // call of fmt.Println copies lock value: sync.WaitGroup contains sync.noCopy
In this case, go vet will warn you about 3 different spots where the copying happens. You can try it yourself at: Go Playground.
Note that it's purely a safeguard for when we're writing and testing our code, we can still run it like normal.
Internal State
The state of a WaitGroup is stored in an atomic.Uint64 variable. You might have guessed this if you've read the mutex post, there are several things packed into this single value.
Here's how it breaks down:
- Counter (high 32 bits): This part keeps track of the number of goroutines the WaitGroup is waiting for. When you call wg.Add() with a positive value, it bumps up this counter, and when you call wg.Done(), it decreases the counter by one.
- Waiter (low 32 bits): This tracks the number of goroutines currently waiting for that counter (the high 32 bits) to hit zero. Every time you call wg.Wait(), it increases this "waiter" count. Once the counter reaches zero, it releases all the goroutines that were waiting.
Then there's the final field, sema uint32, which is an internal semaphore managed by the Go runtime.
when a goroutine calls wg.Wait() and the counter isn't zero, it increases the waiter count and then blocks by calling runtime_Semacquire(&wg.sema). This function call puts the goroutine to sleep until it gets woken up by a corresponding runtime_Semrelease(&wg.sema) call.
We'll dive deeper into this in another article, but for now, I want to focus on the alignment issues.
Alignment Problem
I know, talking about history might seem dull, especially when you just want to get to the point. But trust me, knowing the past is the best way to understand where we are now.
Let's take a quick look at how WaitGroup has evolved over several Go versions:
I can tell you, the core of WaitGroup (the counter, waiter, and semaphore) hasn't really changed across different Go versions. However, the way these elements are structured has been modified many times.
When we talk about alignment, we're referring to the need for data types to be stored at specific memory addresses to allow for efficient access.
For example, on a 64-bit system, a 64-bit value like uint64 should ideally be stored at a memory address that's a multiple of 8 bytes. The reason is, the CPU can grab aligned data in one go, but if the data isn't aligned, it might take multiple operations to access it.
Now, here's where things get tricky:
On 32-bit architectures, the compiler doesn't guarantee that 64-bit values will be aligned on an 8-byte boundary. Instead, they might only be aligned on a 4-byte boundary.
This becomes a problem when we use the atomic package to perform operations on the state variable. The atomic package specifically notes:
"On ARM, 386, and 32-bit MIPS, it is the caller's responsibility to arrange for 64-bit alignment of 64-bit words accessed atomically via the primitive atomic functions." - atomic package note
What this means is that if we don't align the state uint64 variable to an 8-byte boundary on these 32-bit architectures, it could cause the program to crash.
So, what's the fix? Let's take a look at how this has been handled across different versions.
Go 1.5: state1 [12]byte
I'd recommend taking a moment to guess the underlying logic of this solution as you read the code below, then we'll walk through it together.
type WaitGroup struct { state1 [12]byte sema uint32 } func (wg *WaitGroup) state() *uint64 { if uintptr(unsafe.Pointer(&wg.state1))%8 == 0 { return (*uint64)(unsafe.Pointer(&wg.state1)) } else { return (*uint64)(unsafe.Pointer(&wg.state1[4])) } }
Instead of directly using a uint64 for state, WaitGroup sets aside 12 bytes in an array (state1 [12]byte). This might seem like more than you'd need, but there's a reason behind it.
The purpose of using 12 bytes is to ensure there's enough room to find an 8-byte segment that's properly aligned.
The full post is available here: https://victoriametrics.com/blog/go-sync-waitgroup/
以上是Gosync.WaitGroup 和对齐问题的详细内容。更多信息请关注PHP中文网其他相关文章!

在Debian系统上确保整体安全性对于保护LibOffice等应用程序的运行环境至关重要。以下是一些提高系统安全性的通用建议:系统更新定期更新系统以修补已知的安全漏洞。Debian12.10发布了安全更新,修复了大量安全漏洞,包括一些关键软件包。用户权限管理避免使用root用户进行日常操作,以减少潜在的安全风险。建议创建普通用户并加入sudo组,以限制对系统的直接访问。SSH服务安全配置使用SSH密钥对进行身份认证,禁用root远程登录,并限制空密码登录。这些措施可以增强SSH服务的安全性,防止

在Debian系统上调整Rust编译选项,可以通过多种途径来实现,以下是几种方法的详细说明:使用rustup工具进行配置安装rustup:若你尚未安装rustup,可使用下述命令进行安装:curl--proto'=https'--tlsv1.2-sSfhttps://sh.rustup.rs|sh依照提示完成安装过程。设置编译选项:rustup可用于为不同的工具链和目标配置编译选项。你可以使用rustupoverride命令为特定项目设置编译选项。例如,若想为某个项目设置特定的Rust版

在Debian系统上管理Kubernetes(K8S)节点通常涉及以下几个关键步骤:1.安装和配置Kubernetes组件准备工作:确保所有节点(包括主控节点和工作节点)都已安装Debian操作系统,并且满足安装Kubernetes集群的基本要求,如足够的CPU、内存和磁盘空间。禁用swap分区:为了确保kubelet能够顺利运行,建议禁用swap分区。设置防火墙规则:允许必要的端口,如kubelet、kube-apiserver、kube-scheduler等使用的端口。安装container

在Debian上设置Golang环境时,确保系统安全是至关重要的。以下是一些关键的安全设置步骤和建议,帮助您构建一个安全的Golang开发环境:安全设置步骤系统更新:在安装Golang之前,确保系统是最新的。使用以下命令更新系统软件包列表和已安装的软件包:sudoaptupdatesudoaptupgrade-y防火墙配置:安装并配置防火墙(如iptables)以限制对系统的访问。仅允许必要的端口(如HTTP、HTTPS和SSH)连接。sudoaptinstalliptablessud

在Debian上优化和部署Kubernetes集群的性能是一个涉及多个方面的复杂任务。以下是一些关键的优化策略和建议:硬件资源优化CPU:确保为Kubernetes节点和Pod分配足够的CPU资源。内存:增加节点的内存容量,特别是对于内存密集型应用。存储:使用高性能的SSD存储,避免使用网络文件系统(如NFS),因为它们可能会引入延迟。内核参数优化编辑/etc/sysctl.conf文件,添加或修改以下参数:net.core.somaxconn:65535net.ipv4.tcp_max_syn

在Debian系统中,你可以利用cron来安排定时任务,实现Python脚本的自动化执行。首先,启动终端。通过输入以下命令,编辑当前用户的crontab文件:crontab-e如果需要以root权限编辑其他用户的crontab文件,请使用:sudocrontab-uusername-e将username替换为你要编辑的用户名。在crontab文件中,你可以添加定时任务,格式如下:*****/path/to/your/python-script.py这五个星号分别代表分钟(0-59)、小

在Debian系统中调整Golang的网络参数可以通过多种方式实现,以下是几种可行的方法:方法一:通过设置环境变量临时设置环境变量:在终端中输入以下命令可以临时设置环境变量,此设置仅在当前会话有效。exportGODEBUG="gctrace=1netdns=go"其中,gctrace=1会激活垃圾回收跟踪,netdns=go则使Go使用其自身的DNS解析器而非系统默认的。永久设置环境变量:将上述命令添加到你的shell配置文件中,例如~/.bashrc或~/.profile

在Debian系统上自定义LibOffice的快捷键可以通过系统设置进行调整。以下是一些常用的步骤和方法来设置LibOffice的快捷键:设置LibOffice快捷键的基本步骤打开系统设置:在Debian系统中,点击左上角的菜单(通常是一个齿轮图标),然后选择“系统设置”。选择设备:在系统设置窗口中,选择“设备”。选择键盘:在设备设置页面中,选择“键盘”。找到对应工具的命令:在键盘设置页面中,向下滚动到最底部可以看到“快捷键”选项,点击它会弹出一个窗口。在弹出的窗口中找到对应LibOffice工


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

Video Face Swap
使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

热工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

SublimeText3 英文版
推荐:为Win版本,支持代码提示!

记事本++7.3.1
好用且免费的代码编辑器

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

安全考试浏览器
Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。