您的位置 首页 golang

golang面试题8之map读写锁相关问题

下⾯的代码有什么问题?

 package test2

import "sync"

type UserAges struct {
ages map[string]int
sync.Mutex
}

func (ua *UserAges) Add(name string, age int) {
ua.Lock()
defer ua.Unlock()
ua.ages[name] = age
}

func (ua *UserAges) Get(name string) int {
if age, ok := ua.ages[name]; ok {
return age
}
return -1
}  

在执⾏ Get⽅法时可能被panic。

虽然有使⽤sync.Mutex做写锁,但是map是并发读写不安全的。map属于引⽤类型,并

发读写时多个协程⻅是通过指针访问同⼀个地址,即访问共享变量,此时同时读写资源

存在竞争关系。会报错误信息:“fatal error: concurrent map read and map write”。

因此,在 Get 中也需要加锁,因为这⾥只是读,建议使⽤读写锁 sync.RWMutex 。

文章来源:智云一二三科技

文章标题:golang面试题8之map读写锁相关问题

文章地址:https://www.zhihuclub.com/94608.shtml

关于作者: 智云科技

热门文章

网站地图