并发安全容器新增LockFunc/RLockFunc方法,完善Iterator遍历方法注释

This commit is contained in:
John
2018-04-15 22:02:06 +08:00
parent 10b6f8dda0
commit 5494a4d710
19 changed files with 203 additions and 32 deletions

View File

@ -22,7 +22,7 @@ func NewStringBoolMap() *StringBoolMap {
}
}
// 给定回调函数对原始内容进行遍历
// 给定回调函数对原始内容进行遍历回调函数返回true表示继续遍历否则停止遍历
func (this *StringBoolMap) Iterator(f func (k string, v bool) bool) {
this.mu.RLock()
for k, v := range this.m {
@ -159,3 +159,17 @@ func (this *StringBoolMap) Clear() {
this.m = make(map[string]bool)
this.mu.Unlock()
}
// 使用自定义方法执行加锁修改操作
func (this *StringBoolMap) LockFunc(f func(m map[string]bool)) {
this.mu.Lock()
f(this.m)
this.mu.Unlock()
}
// 使用自定义方法执行加锁读取操作
func (this *StringBoolMap) RLockFunc(f func(m map[string]bool)) {
this.mu.RLock()
f(this.m)
this.mu.RUnlock()
}