這篇文章是關於 Go 中處理並發的系列文章的一部分:
- Gosync.Mutex:正常與飢餓模式
- Gosync.WaitGroup 和對齊問題(我們在這裡)
- Gosync.Pool 及其背後的機制
- Gosync.Cond,最被忽略的同步機制
- Gosync.Map:適合正確工作的正確工具
- Go Singleflight 融入您的程式碼,而不是您的資料庫
WaitGroup 基本上是一種等待多個 goroutine 完成其工作的方法。
每個同步原語都有自己的一系列問題,這也不例外。我們將重點放在 WaitGroup 的對齊問題,這就是為什麼它的內部結構在不同版本中變化的原因。
本文以 Go 1.23 為基礎。如果後續有任何變化,請隨時透過 X(@func25) 告訴我。
什麼是sync.WaitGroup?
如果您已經熟悉sync.WaitGroup,請隨意跳過。
讓我們先深入探討這個問題,想像您手上有一項艱鉅的工作,因此您決定將其分解為可以同時運行且彼此不依賴的較小任務。
為了解決這個問題,我們使用 goroutine,因為它們讓這些較小的任務同時運行:
func main() { for i := 0; i <p>但是事情是這樣的,<strong>很有可能</strong>主協程在其他協程完成工作之前完成並退出。 </p> <p>當我們分出許多 goroutine 來做他們的事情時,我們希望跟踪它們,以便主 goroutine 不會在其他人完成之前完成並退出。這就是 WaitGroup 發揮作用的地方。每次我們的一個 goroutine 完成其任務時,它都會讓 WaitGroup 知道。 </p> <p>一旦所有 goroutine 都簽入為“完成”,主 goroutine 就知道可以安全完成,並且一切都會整齊地結束。 <br> </p> <pre class="brush:php;toolbar:false">func main() { var wg sync.WaitGroup wg.Add(10) for i := 0; i <p>所以,通常是這樣的:</p>
- 新增 goroutine:在啟動 goroutine 之前,您需要告訴 WaitGroup 需要多少個 goroutine。您可以使用 WaitGroup.Add(n) 來執行此操作,其中 n 是您計劃運行的 goroutine 數量。
- Goroutines running:每個 Goroutine 都會開始執行它的任務。完成後,它應該透過呼叫 WaitGroup.Done() 來讓 WaitGroup 知道,以將計數器減一。
- 等待所有 goroutine:在主 goroutine 中,即不執行繁重工作的 goroutine,您調用 WaitGroup.Wait()。這會暫停主 goroutine,直到 WaitGroup 中的計數器達到零。簡單來說,它會等待所有其他 goroutine 完成並發出完成信號。
通常,你會看到在啟動 goroutine 時使用 WaitGroup.Add(1):
for i := 0; i <p>這兩種方法在技術上都很好,但是使用 wg.Add(1) 會對效能造成很小的影響。儘管如此,與使用 wg.Add(n).</p> 相比,它更不容易出錯 <blockquote> <p><em>「為什麼 wg.Add(n) 被認為容易出錯?」</em></p> </blockquote> <p>重點是,如果循環的邏輯發生變化,就像有人添加了跳過某些迭代的 continue 語句,事情可能會變得混亂:<br> </p> <pre class="brush:php;toolbar:false">wg.Add(10) for i := 0; i <p>在這個例子中,我們在循環之前使用 wg.Add(n) ,假設循環總是恰好啟動 n 個 goroutine。 </p> <p>但是如果這個假設不成立,例如跳過一些迭代,你的程式可能會陷入等待從未啟動的 goroutine 的狀態。老實說,這種錯誤追蹤起來確實很痛苦。 </p> <p>這種情況下,wg.Add(1) 比較合適。它可能會帶來一點點效能開銷,但它比處理人為錯誤開銷要好得多。 </p> <p>人們在使用sync.WaitGroup時也常犯一個很常見的錯誤:<br> </p> <pre class="brush:php;toolbar:false">for i := 0; i <p>歸根結底,wg.Add(1) 正在<strong>內部</strong> goroutine 中呼叫。這可能是一個問題,因為 Goroutine 可能在主 Goroutine 已經調用 wg.Wait() 之後開始運行。 </p> <p>這可能會導致各種計時問題。另外,如果您注意到,上面的所有範例都使用 defer 和 wg.Done()。它確實應該與 defer 一起使用,以避免多個返迴路徑或恐慌恢復的問題,確保它總是被呼叫並且不會無限期地阻止呼叫者。 </p> <p>這應該涵蓋所有基礎知識。 </p> <h2> sync.WaitGroup 是什麼樣子的? </h2> <p>我們先查看sync.WaitGroup的原始碼。您會在sync.Mutex 中註意到類似的模式。 </p> <blockquote> <p>再次強調,如果您不熟悉互斥鎖的工作原理,我強烈建議您先查看這篇文章:Go Sync Mutex:正常模式和飢餓模式。 <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() {}
在 Go 中,只需將結構分配給另一個變數即可輕鬆複製結構。但有些結構,例如 WaitGroup,確實不應該被複製。
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中文網其他相關文章!

本文解釋了GO的軟件包導入機制:命名imports(例如導入“ fmt”)和空白導入(例如導入_ fmt; fmt;)。 命名導入使包裝內容可訪問,而空白導入僅執行t

本文詳細介紹了MySQL查詢結果的有效轉換為GO結構切片。 它強調使用數據庫/SQL的掃描方法來最佳性能,避免手動解析。 使用DB標籤和Robus的結構現場映射的最佳實踐

本文解釋了Beego的NewFlash()函數,用於Web應用程序中的頁間數據傳輸。 它專注於使用newflash()在控制器之間顯示臨時消息(成功,錯誤,警告),並利用會話機制。 Lima

本文演示了創建模擬和存根進行單元測試。 它強調使用接口,提供模擬實現的示例,並討論最佳實踐,例如保持模擬集中並使用斷言庫。 文章

本文探討了GO的仿製藥自定義類型約束。 它詳細介紹了界面如何定義通用功能的最低類型要求,從而改善了類型的安全性和代碼可重複使用性。 本文還討論了局限性和最佳實踐

本文詳細介紹了在GO中詳細介紹有效的文件,將OS.WriteFile(適用於小文件)與OS.openfile和緩衝寫入(最佳大型文件)進行比較。 它強調了使用延遲並檢查特定錯誤的可靠錯誤處理。

本文使用跟踪工具探討了GO應用程序執行流。 它討論了手冊和自動儀器技術,比較諸如Jaeger,Zipkin和Opentelemetry之類的工具,並突出顯示有效的數據可視化


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

AI Hentai Generator
免費產生 AI 無盡。

熱門文章

熱工具

SublimeText3 Linux新版
SublimeText3 Linux最新版

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

SublimeText3漢化版
中文版,非常好用

記事本++7.3.1
好用且免費的程式碼編輯器

Dreamweaver Mac版
視覺化網頁開發工具