Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于ConcurrentDict的实现问题。 #227

Open
1024wangxiao opened this issue Jul 23, 2024 · 0 comments
Open

关于ConcurrentDict的实现问题。 #227

1024wangxiao opened this issue Jul 23, 2024 · 0 comments

Comments

@1024wangxiao
Copy link

博客中描述的ConcurrentMap使用分段式锁,把key 分散到固定数量的 shard shard 是有锁保护的 map, 当 shard 进行 rehash 时会阻塞shard内的读写(重点是读写都阻塞),但不会对其他 shard 造成影响。
在博客的讲解中使用的是读锁针对Get方法进行操作,如下:
func (dict *ConcurrentDict) Get(key string) (val interface{}, exists bool) {
if dict == nil {
panic("dict is nil")
}
hashCode := fnv32(key)
index := dict.spread(hashCode)
shard := dict.getShard(index)
shard.mutex.RLock()
defer shard.mutex.RUnlock()
val, exists = shard.m[key]
return
}
但是在您的项目源码中为什么使用的是互斥方式?
func (dict *ConcurrentDict) Get(key string) (val interface{}, exists bool) {
if dict == nil {
panic("dict is nil")
}
hashCode := fnv32(key)
index := dict.spread(hashCode)
s := dict.getShard(index)
s.mutex.Lock() // 对分片加读锁
defer s.mutex.Unlock() // 读锁结束时解锁
val, exists = s.m[key]
return
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant