猿问

带有sync.waitGroup的Goroutine每次输出不同的值

下面的代码每次执行后都会打印不同的值,但我希望这些值相同,如何在不使用的情况下更改下面的代码time.Sleep


package main


import (

    "fmt"

    "sync"

)


var total int

var wg sync.WaitGroup


// Inc increments the counter for the given key.

func inc(num int) {

    total += num

    wg.Done()

}


// Value returns the current value of the counter for the given key.

func getValue() int {

    return total

}


func main() {

    for i := 1; i <= 1000; i++ {

        wg.Add(1)

        go inc(i)

    }

    wg.Wait()

    fmt.Println(getValue())

}


慕桂英546537
浏览 100回答 3
3回答

幕布斯7119047

你有一个数据竞赛,结果是不确定的。您必须同步对共享变量的访问:var total intvar lock sync.Mutexvar wg sync.WaitGroup// Inc increments the counter for the given key.func inc(num int) {&nbsp; &nbsp; lock.Lock()&nbsp; &nbsp; defer lock.Unlock()&nbsp; &nbsp; total += num&nbsp; &nbsp; wg.Done()}// Value returns the current value of the counter for the given key.func getValue() int {&nbsp; &nbsp; lock.Lock()&nbsp; &nbsp; defer lock.Unlock()&nbsp; &nbsp; return total}或者,用于sync/atomic访问/修改变量。

慕虎7371278

已经提到您有“数据竞赛”,使用Mutex是一种解决方案。或者,您可以使用atomic更快的包。package mainimport (&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "sync"&nbsp; &nbsp; "sync/atomic")var total uint64var wg sync.WaitGroupfunc inc(num uint64) {&nbsp; &nbsp; atomic.AddUint64(&total, 1)&nbsp; &nbsp; wg.Done()}// Value returns the current value of the counter for the given key.func getValue() uint64 {&nbsp; &nbsp; return atomic.LoadUint64(&total)}func main() {&nbsp; &nbsp; for i := uint64(1); i <= 1000; i++ {&nbsp; &nbsp; &nbsp; &nbsp; wg.Add(1)&nbsp; &nbsp; &nbsp; &nbsp; go inc(i)&nbsp; &nbsp; }&nbsp; &nbsp; wg.Wait()&nbsp; &nbsp; fmt.Println(getValue())}

慕桂英3389331

每次获得不同值的原因是total += num.一个简单的解决方法是添加互斥锁: var mu sync.Mutex并将其用于inc :func inc(num int) {&nbsp; &nbsp; mu.Lock()&nbsp; &nbsp; defer mu.Unlock()&nbsp; &nbsp; total += num&nbsp; &nbsp; wg.Done()}
随时随地看视频慕课网APP

相关分类

Go
我要回答