首頁 > 後端開發 > Golang > 主體

使用帶有sync.WaitGroup的goroutine結果不一致

WBOY
發布: 2024-02-09 10:30:10
轉載
996 人瀏覽過

使用帶有sync.WaitGroup的goroutine結果不一致

在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中文網其他相關文章!

相關標籤:
來源:stackoverflow.com
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!