英文:
concurrent map iteration and map write error in Golang 1.8
问题
所以我有这个函数..
func Set(firstSet map[string][]App, store map[string]*Parsed) map[string][string]struct{} {
s := make(map[string]map[string]struct{})
for dmn, parsed := range store {
for cId, apps := range firstSet {
if _, ok := s[dmn]; !ok {
s[dmn] = make(map[string]struct{})
}
s[dmn][cId] = struct{}{}
}
}
return s
}
该函数的第3行(for dmn, parsed := range store)在Golang 1.8中给我报错"concurrent map iteration and map write error",有什么想法吗?
英文:
So I have this func..
func Set(firstSet map[string][]App, store map[string]*Parsed) map[string][string]struct{} {
s := make(map[string]map[string]struct{})
for dmn, parsed := range store {
for cId, apps := range firstSet {
if _, ok := s[dmn]; !ok {
s[dmn] = make(map[string]struct{})
}
s[dmn][cId] = struct{}{}
}
}
return s
}
Line 3 of that func (for dmn, parsed := range store) is giving me the error concurrent map iteration and map write error in Golang 1.8. any idea?
答案1
得分: 5
看起来像是并发地图滥用。可能是因为你的函数被不同的goroutine调用。尝试在函数体中使用mutex.Lock()/Unlock()来保证你的地图在并发使用时是安全的。
英文:
It looks like Concurrent Map Misuse . Probably your function invoked from different gorotines. Try to enclose function body in mutex.Lock()/Unlock() so that your map is safe for concurrent use.
答案2
得分: 2
在Golang 1.8中添加了一个增强的并发访问检查,以下是在runtime/hashmap.go的第736行的源代码:
if h.flags&hashWriting != 0 {
throw("concurrent map iteration and map write")
}
英文:
There is a enhanced concurrent access check added in the Golang 1.8, and this is the source code in runtime/hashmap.go:736,
if h.flags&hashWriting != 0 {
throw("concurrent map iteration and map write")
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论