为什么标志“-race”的结果与预期的不一样? 它期望相同的结果:1000000 - 带有标志“-race”但没有这个
https://gist.github.com/romanitalian/f403ceb6e492eaf6ba953cf67d5a22ff
package main
import (
"fmt"
"runtime"
"sync/atomic"
"time"
)
//$ go run -race main_atomic.go
//954203
//
//$ go run main_atomic.go
//1000000
type atomicCounter struct {
val int64
}
func (c *atomicCounter) Add(x int64) {
atomic.AddInt64(&c.val, x)
runtime.Gosched()
}
func (c *atomicCounter) Value() int64 {
return atomic.LoadInt64(&c.val)
}
func main() {
counter := atomicCounter{}
for i := 0; i < 100; i++ {
go func(no int) {
for i := 0; i < 10000; i++ {
counter.Add(1)
}
}(i)
}
time.Sleep(time.Second)
fmt.Println(counter.Value())
}
最佳答案
之所以结果不一样是因为 time.Sleep(time.Second)
不保证你所有的 goroutines 都会在一秒的时间跨度内执行。即使执行 go run main.go
,也不能保证每次都会得到相同的结果。如果将 time.Milisecond
替换为 time.Second
,您可以对此进行测试,您会看到更加不一致的结果。
无论您在 time.Sleep
方法中输入什么值,它都不能保证您的所有 goroutines 都会被执行,它只是意味着您的所有 goroutines 都不太可能不会完成及时。
为了获得一致的结果,您可能需要稍微同步一下您的 goroutine。您可以使用 WaitGroup
或 channel 。
使用 WaitGroup
:
//rest of the code above is the same
func main() {
counter := atomicCounter{}
var wg sync.WaitGroup
for i := 0; i < 100; i++ {
wg.Add(1)
go func(no int) {
for i := 0; i < 10000; i++ {
counter.Add(1)
}
wg.Done()
}(i)
}
wg.Wait()
fmt.Println(counter.Value())
}
有 channel :
func main() {
valStream := make(chan int)
doneStream := make(chan int)
result := 0
for i := 0; i < 100; i++ {
go func() {
for i := 0; i < 10000; i++ {
valStream <- 1
}
doneStream <- 1
}()
}
go func() {
counter := 0
for count := range doneStream {
counter += count
if counter == 100 {
close(doneStream)
}
}
close(valStream)
}()
for val := range valStream {
result += val
}
fmt.Println(result)
}
关于go - 为什么结果与标志 "-race"不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50050755/