问题

map并发读写,会出现:fatal error: concurrent map read and map write,源码:

const (
  ...
	hashWriting  = 4 // a goroutine is writing to the map
	...
)

type hmap struct {
	...
	flags     uint8
	...
}

map是检查是否有另外线程修改h.flag来判断,是否有并发问题。

// 在更新map的函数里检查并发写
	if h.flags&hashWriting == 0 {
		throw("concurrent map writes")
	}
	
// 在读map的函数里检查是否有并发写
	if h.flags&hashWriting != 0 {
		throw("concurrent map read and map write")
	}

原因

为什么 map 并发写会导致这个错误?

因为 map 变量为 指针类型变量,并发写时,多个协程同时操作一个内存,类似于多线程操作同一个资源会发生竞争关系,共享资源会遭到破坏,因此golang 出于安全的考虑,抛出致命错误:fatal error: concurrent map writes。

类似的,slice和map都会产生并发读写问题

可以加锁写map

currMap := make(map[int64]int64)
wg := eg.WithContext(ctx)
mutex := sync.Mutex{}

for _, uid := range uids {
	wg.Go(func(ctx context.Context) error {
		resp, err := 下游服务接口(ctx, uid)
		if err != nil {
			...
		}

		mutex.Lock()
		defer mutex.Unlock()

		currMap[uid] = resp

		return nil
	})

}

sync.map,及其原理

var sMap sync.Map
sMap.Store() // set
sMap.Load() // get