
在Go语言中,使用goroutine可以实现并发执行任务,而sync.WaitGroup则是一种同步机制,用于等待一组goroutine的完成。然而,php小编香蕉发现,在某些情况下,使用带有sync.WaitGroup的goroutine可能会导致结果不一致的问题。这种问题通常发生在多个goroutine同时修改共享变量的情况下,由于goroutine的执行顺序不确定,可能会导致最终结果的不一致性。在本文中,我们将探讨这个问题的原因,并提供一些解决方案来确保goroutine之间的结果一致性。
我正在尝试使用 goroutine(在 Go lang 中)计算小于任意整数 i 的素数数量。
例如,如果 i 为 100,则结果应为 25。
以下是我当前的实现:
<code>package "main"
import (
"fmt"
"math"
"sync"
"time"
)
var wg sync.WaitGroup
func isprime(x int) bool {
if x == 2 {
return true
}
if x == 1 || x%2 == 0 {
return false
}
var xi = float64(x)
for i := 3; float64(i) < (math.Pow(xi, 0.5) + 1.0); i += 2.0 {
if x%i == 0 {
return false
}
}
return true
}
func main() {
fmt.Print("Till what number should I count primes? ")
var i int
fmt.Scan(&i)
r := 0
pr := &r
fmt.Println("Counting primes till ", i)
start := time.Now()
for x := 0; x <= i; x++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
if isprime(n) {
*pr += 1
}
}(x)
}
wg.Wait()
elapsed := time.Since(start).Seconds()
fmt.Println("Counted", r, "primes")
fmt.Println("took", elapsed, "seconds")
}
</code>当我运行这个程序时,我得到了较小的 i 值的正确结果(直到大约 1000)
但是对于较大的 i 值,结果不一致并且不正确。
❯ ./main Till what number should I count primes? 10000 Counting primes till 10000 Counted 1228 primes took 0.006776541 seconds ❯ ./main Till what number should I count primes? 10000 Counting primes till 10000 Counted 1227 primes took 0.004183875 seconds ❯ ./main Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78254 primes took 0.441985921 seconds ❯ ./main Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78327 primes took 0.430042047 seconds
随着 i 的值变大,结果波动增大。是什么原因造成的?有什么方法可以使其一致且正确吗?
您有一个共享变量,但没有适当的同步。存在竞争条件(*pr += 1)。在共享变量前后添加互斥体修复它(mu.Lock()、mu.Unlock())。
代码:
var wg sync.WaitGroup
var mu sync.Mutex
func main() {
fmt.Print("Till what number should I count primes? ")
var i int
fmt.Scan(&i)
r := 0
pr := &r
fmt.Println("Counting primes till ", i)
start := time.Now()
for x := 0; x <= i; x++ {
wg.Add(1)
go func(n int) {
defer wg.Done()
if isprime(n) {
mu.Lock() // <= lock
*pr += 1
mu.Unlock() // <= unlock
}
}(x)
}
wg.Wait()
elapsed := time.Since(start).Seconds()
fmt.Println("Counted", r, "primes")
fmt.Println("took", elapsed, "seconds")
}输出:
Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78498 primes took 0.6783484 seconds Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78498 primes took 0.5428273 seconds Till what number should I count primes? 1000000 Counting primes till 1000000 Counted 78498 primes took 0.5521617 seconds
以上就是使用带有sync.WaitGroup的goroutine结果不一致的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号