新增 AtomGetSet 函数

This commit is contained in:
kercylan98 2023-05-08 14:15:15 +08:00
parent fbcea9fba5
commit 00c4534574
1 changed files with 10 additions and 0 deletions

View File

@ -26,6 +26,16 @@ func (slf *Map[Key, Value]) Get(key Key) Value {
return slf.data[key]
}
// AtomGetSet 原子方式获取一个值并在之后进行赋值
func (slf *Map[Key, Value]) AtomGetSet(key Key, handle func(value Value, exist bool) (newValue Value, isSet bool)) {
slf.lock.Lock()
defer slf.lock.Unlock()
value, exist := slf.data[key]
if newValue, isSet := handle(value, exist); isSet {
slf.data[key] = newValue
}
}
func (slf *Map[Key, Value]) Exist(key Key) bool {
slf.lock.RLock()
_, exist := slf.data[key]