Thread-Safe-Dictionary icon indicating copy to clipboard operation
Thread-Safe-Dictionary copied to clipboard

removeValue and removeAll are async, which breaks the assumption of Dictionary

Open nikbhatt-cu opened this issue 2 years ago • 1 comments

The implementation of removeValue and removeAll uses self.concurrentQueue.async, which results in a different behavior vs. the regular dictionary. This means it's not a fully drop-in replacement.

If a caller removes a value and then that caller (or a caller of the caller) checks the dictionary, it may still contain the value (for an indeterminate amount of time).

May I ask why these calls must be async? Thanks.

nikbhatt-cu avatar Oct 08 '23 22:10 nikbhatt-cu

From my understanding, the barrier flag will prevent your concern. Checking the value will have to wait until the removal is done. Without the barrier flag, your concern would be correct.

In this code example the result of print is nil.

let tsDic = ThreadSafeDictionary<Int, String>()
tsDic[1] = "1"
tsDic[2] = "2"

tsDic.removeValue(forKey: 1)
print(tsDic[1])

If you remove the barrier flag then it would be "1".

But you are correct in that removeValue should return the value.

kristofk avatar Feb 21 '25 08:02 kristofk