在Golang 1.8中出现了并发地遍历映射和写入映射的错误。

huangapple go评论84阅读模式
英文:

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")
}

huangapple
  • 本文由 发表于 2017年2月25日 15:52:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/42453442.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定