go|sync系列:WaitGroup、Once、Cond
以下是针对Go语言中的sync
包中的WaitGroup
、Once
和Cond
三个组件的简单示例代码。
WaitGroup
示例:
package main
import (
"fmt"
"sync"
"time"
)
func worker(id int, wg *sync.WaitGroup) {
defer wg.Done() // 确保每个goroutine结束时都调用Done
fmt.Printf("Worker %d starting\n", id)
time.Sleep(time.Second)
fmt.Printf("Worker %d done\n", id)
}
func main() {
var wg sync.WaitGroup
for i := 1; i <= 5; i++ {
wg.Add(1) // 为每个goroutine增加计数
go worker(i, &wg)
}
wg.Wait() // 等待所有goroutine完成
}
Once
示例:
package main
import (
"fmt"
"sync"
)
func main() {
var once sync.Once
var count int
increment := func() {
count++
fmt.Println("Increment called:", count)
}
for i := 0; i < 10; i++ {
go func() {
once.Do(increment)
}()
}
// 等待goroutines完成
time.Sleep(time.Second)
}
Cond
示例:
package main
import (
"fmt"
"sync"
"time"
)
func main() {
var cond sync.Cond
cond.L = new(sync.Mutex)
isReady := false
waitForReady := func() {
cond.L.Lock()
for !isReady {
fmt.Println("Waiting for ready signal...")
cond.Wait()
}
fmt.Println("Received ready signal.")
cond.L.Unlock()
}
sendReadySignal := func() {
cond.L.Lock()
isReady = true
cond.Signal()
fmt.Println("Sent ready signal.")
cond.L.Unlock()
}
go waitForReady()
time.Sleep(time.Second) // 确保goroutine有足够的时间等待
go sendReadySignal()
// 等待goroutines完成
time.Sleep(2*time.Second)
}
这些示例代码展示了如何使用sync
包中的WaitGroup
、Once
和Cond
来同步并发的goroutines。WaitGroup
用于等待一组goroutines完成;Once
确保某个函数只执行一次;而Cond
用于在满足特定条件时唤醒等待的goroutines。
评论已关闭