We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
博客中描述的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 }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
博客中描述的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
}
The text was updated successfully, but these errors were encountered: