#### golang map并发的问题 以及sync.map ####

问题

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

sync.map的使用【sync.Map_wangfy_的博客-CSDN博客

sync.map的原理【golang里sync.map的原理和使用_wangfy_的博客-CSDN博客_golang sync.map原理

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值