我注意到如果我尝试在GearItem
循环中使用goroutine附加到切片,则会出现我会丢失/空白数据的情况:
Activity
有时,当我从for
打印所有destSlice := make([]myClass, 0)
var wg sync.WaitGroup
for _, myObject := range sourceSlice {
wg.Add(1)
go func(closureMyObject myClass) {
defer wg.Done()
var tmpObj myClass
tmpObj.AttributeName = closureMyObject.AttributeName
destSlice = append(destSlice, tmpObj)
}(myObject)
}
wg.Wait()
时,有些元素是空字符串(AttributeName
),有时候,destSlice
中的某些元素不存在在""
。
我的代码是否有数据争用,这是否意味着sourceSlice
对多个goroutine并发使用不是线程安全的?
答案 0 :(得分:18)
在Go中,没有值对于并发读/写是安全的,切片(slice headers)也不例外。
是的,您的代码有数据竞争。使用-race
选项运行以验证。
示例:
type myClass struct {
AttributeName string
}
sourceSlice := make([]myClass, 100)
destSlice := make([]myClass, 0)
var wg sync.WaitGroup
for _, myObject := range sourceSlice {
wg.Add(1)
go func(closureMyObject myClass) {
defer wg.Done()
var tmpObj myClass
tmpObj.AttributeName = closureMyObject.AttributeName
destSlice = append(destSlice, tmpObj)
}(myObject)
}
wg.Wait()
使用
运行它go run -race play.go
输出是:
==================
WARNING: DATA RACE
Read at 0x00c420074000 by goroutine 6:
main.main.func1()
/home/icza/gows/src/play/play.go:20 +0x69
Previous write at 0x00c420074000 by goroutine 5:
main.main.func1()
/home/icza/gows/src/play/play.go:20 +0x106
Goroutine 6 (running) created at:
main.main()
/home/icza/gows/src/play/play.go:21 +0x1cb
Goroutine 5 (running) created at:
main.main()
/home/icza/gows/src/play/play.go:21 +0x1cb
==================
==================
WARNING: DATA RACE
Read at 0x00c42007e000 by goroutine 6:
runtime.growslice()
/usr/local/go/src/runtime/slice.go:82 +0x0
main.main.func1()
/home/icza/gows/src/play/play.go:20 +0x1a7
Previous write at 0x00c42007e000 by goroutine 5:
main.main.func1()
/home/icza/gows/src/play/play.go:20 +0xc4
Goroutine 6 (running) created at:
main.main()
/home/icza/gows/src/play/play.go:21 +0x1cb
Goroutine 5 (running) created at:
main.main()
/home/icza/gows/src/play/play.go:21 +0x1cb
==================
==================
WARNING: DATA RACE
Write at 0x00c420098120 by goroutine 80:
main.main.func1()
/home/icza/gows/src/play/play.go:20 +0xc4
Previous write at 0x00c420098120 by goroutine 70:
main.main.func1()
/home/icza/gows/src/play/play.go:20 +0xc4
Goroutine 80 (running) created at:
main.main()
/home/icza/gows/src/play/play.go:21 +0x1cb
Goroutine 70 (running) created at:
main.main()
/home/icza/gows/src/play/play.go:21 +0x1cb
==================
Found 3 data race(s)
exit status 66
解决方案很简单,使用sync.Mutex
来保护写destSlice
值:
destSlice := make([]myClass, 0)
mux := &sync.Mutex{}
var wg sync.WaitGroup
for _, myObject := range sourceSlice {
wg.Add(1)
go func(closureMyObject myClass) {
defer wg.Done()
var tmpObj myClass
tmpObj.AttributeName = closureMyObject.AttributeName
mux.Lock()
destSlice = append(destSlice, tmpObj)
mux.Unlock()
}(myObject)
}
wg.Wait()
您也可以通过其他方式解决此问题,例如:您可以使用您要发送附加值的频道,并从该频道接收指定的goroutine并进行追加。
答案 1 :(得分:5)
这是一个很老的问题,但是还有一个小的改进可以帮助消除互斥量。您可以使用索引添加到数组。每个go例程将使用其自己的索引。在这种情况下,同步是不必要的。
{{#if: {destSlice := make([]myClass, len(sourceSlice))
var wg sync.WaitGroup
for i, myObject := range sourceSlice {
wg.Add(1)
go func(idx int, closureMyObject myClass) {
defer wg.Done()
var tmpObj myClass
tmpObj.AttributeName = closureMyObject.AttributeName
destSlice[idx] = tmpObj
}(i, myObject)
}
wg.Wait()
} | {{{1}}}} | Example is absent. }}
答案 2 :(得分:1)
要为该问题提供最新的解决方案,看起来Go出于同步目的发布了新地图:
答案 3 :(得分:1)
问题已得到解答,但我最喜欢的解决方法是使用 errgroup。 docs 中的示例之一就是这个确切的问题加上错误处理的一个很好的补充。
以下是文档中示例的主要内容:
g, ctx := errgroup.WithContext(ctx)
searches := []Search{Web, Image, Video}
results := make([]Result, len(searches))
for i, search := range searches {
i, search := i, search // https://golang.org/doc/faq#closures_and_goroutines
g.Go(func() error {
result, err := search(ctx, query)
if err == nil {
results[i] = result
}
return err
})
}
if err := g.Wait(); err != nil {
return nil, err
}
return results, nil
希望这对那些不知道 errgroup 包的人有所帮助。