每个地图键都有专属锁
时间:2024-02-24
浏览:500
作者:佚名
大家好,我们又见面了啊~本文《每个地图键都有专属锁》的内容中将会涉及到等等。如果你正在学习Golang相关知识,欢迎关注我,以后会给大家带来更多Golang相关文章,希望我们能一起进步!下面就开始本文的正式内容~
问题内容
我有一个地图(实体),其中键是字符串,值是结构。构建该结构是一项昂贵的操作,因为它必须转到数据库来加载数据。
type entitystatemanagers struct {
entities map[string]*entitystatemanager
mainlock *sync.mutex
locks map[string]*sync.mutex
}
下面的函数返回所提供键的结构。如果该结构不在内存中,它将从数据库加载它。
我不想对所有内容都只有一把锁,因为这会在 expenseload(entityid) 运行时完全阻止对地图的访问。
我尝试使用一个单独的结构(锁),每个映射键有一个锁。例如,只有访问 entity_123 时,才需要等待 exppressiveload(entity_123)。
我还需要做更多测试,但它似乎有效。
func (handler entityStateManagers) getStateManager(entityId string) (*entityStateManager, error) {
handler.mainLock.Lock()
if handler.locks[entityId] == nil {
handler.locks[entityId] = &sync.Mutex{}
}
handler.mainLock.Unlock()
handler.locks[entityId].Lock()
defer handler.locks[entityId].Unlock()
if handler.entities[entityId] == nil {
handler.entities[entityId] = expensiveLoad(entityId)
}
return handler.entities[entityId], nil
}
这是一个合理的方法吗?我有什么遗漏的吗?有更好的方法吗?
解决方案
我会这样说,但我可能是错的,保留两个具有相同键的映射是没有用的,指向互斥体的指针也是呃。
type Wrapper struct {
*entityStateManager
sync.Mutex
}
type entityStateManagers struct {
entities map[string]*Wrapper
mainLock sync.Mutex
}
func (handler entityStateManagers) getStateManager(entityId string) (*entityStateManager, error) {
handler.mainLock.Lock()
val, ok := handler.entities[entityId]
if !ok {
val = &Wrapper{}
handler.entities[entityId] = val
}
val.Lock()
handler.mainLock.Unlock()
val.entityStateManager = expensiveLoad(entityId)
defer val.Unlock()
return val.entityStateManager, nil
}今天关于《每个地图键都有专属锁》的内容介绍就到此结束,如果有什么疑问或者建议,可以在golang学习网公众号下多多回复交流;文中若有不正之处,也希望回复留言以告知!
本文内容来源于互联网,如有侵权请联系删除。
近期热门
11.18
11.15
站内热门
最新文章
11.29
11.29
11.29
11.29
11.29
热门专题
