diff --git a/Sources/ConcurrencyHelpers/DictionaryWithLock.swift b/Sources/ConcurrencyHelpers/DictionaryWithLock.swift new file mode 100644 index 0000000..0c09e77 --- /dev/null +++ b/Sources/ConcurrencyHelpers/DictionaryWithLock.swift @@ -0,0 +1,20 @@ +public struct DictionaryWithLock { + 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 + } + } + } +}