Skip to content

Commit

Permalink
feat: [sc-741] add helper for map with lock (#22)
Browse files Browse the repository at this point in the history
* feat: [sc-741] add helper for dictionary with lock
  • Loading branch information
mr-swifter authored Oct 11, 2022
1 parent 23c503f commit 8ea4e80
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions Sources/ConcurrencyHelpers/DictionaryWithLock.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public struct DictionaryWithLock<K: Hashable, V> {
var lock: ConcurrencyHelpers.Lock = .init()
var map: [K: V] = [:]

public subscript(key: K) -> V? {
get {
lock.withLock {
guard let value = map[key] else {
return nil
}
return value
}
}
set {
lock.withLock {
map[key] = newValue
}
}
}
}

0 comments on commit 8ea4e80

Please sign in to comment.