Why is this not thread safe?
Originally posted at 4/15/2011
Take a look at the following piece of code:
if(items.ContainsKey(key) == false) { lock(items) { if(items.ContainsKey(key) == false) { items.Add(key, val); } } }
The answer is quite simple, and it is located directly in the docs:
Yes, on the face of it, this is safe code, in the sense that we will never get DuplicateKeyException. But the implementation of ContainsKey() isn’t safe to run when Add() is also executing.
The actual behavior depends on the implementation, but it is easy enough to imagine a scenario where invariants that ContainsKey() relies on are broken for the duration of the Add() call.
Comments
For example during an array expansion.
so what's the solution? could you use a resetEvent to only block when writing?
@John the "best" solution is to use ConcurrentDictionary. msdn.microsoft.com/en-us/library/dd287191.aspx
Just inner part works fine:
lock(items)
@Alex, that's the most pessimistic approach though.
So this code is thread safe, the code that would not be thread safe would be the one enumerating the collection
@Eber, no it's not. The ContainsKey call outside of the lock is not thread safe.
A ReaderWriterLockSlim would be a good solution here.
ConcurrentDictionary is sometimes clunky, it all depends on the rest of the using code; in this example, it would be easy to replace it with TryAdd, but if anyone else uses this dictionary they'd need to change their code. Of course, with a ReaderWriterLock they'd need to acquire an appropriate lock, but it's much easier to avoid issues that way sometimes.
The example code is not thread-safe as the data structure is mutable, and an unlocked read operation is allowed. At any given time other thread(s) may be adding new items to the collection and therefore mutating it. If ContainsKey is executing whist the collection is being mutated, the outcome is indeterminate. For instance ContainsKey may depend on intermediate results as it executes which could become invalid as the collection is mutated by the addition of a new item e.g. the collection may have to be re-arranged to maintain the load factor when a new item is added. This could produce an incorrect result or an exception during the execution of ContainsKey.
@configurator you might want to consider that it is still several times slower to acquire and release a reader writer lock than a standard lock. If the thing you are doing inside the lock is fast, a simple lock should be fine.
You can test it out to see what performance gain you would actually get, my guess is hardly any.
This is a pretty common pitfall. It might show itself as a livelock (very high or 100% CPU in production).
ConcurrentDictionary is the solution, and has a great API too
This code is perfectly thread safe and the documentation supplied does not invalidate the thread safety of the code as I see it. Once 'items' is locked successfully by a thread, no modifications can occur, your reads including enumerations and your if-check will be guaranteed.
@Sony,
No, it isn't thread safe. If thread A enters the lock and ContainsKey returns false, it will call Add. Now if in the middle of the Add (before it completes), thread B calls ContainsKey on the dictionary, the invariants of ContainsKey can be broken. As Ayende states, this is implementation dependent. But consider, what if Add had finished adding the key (so ContainsKey returns true) but not the value. At this point the indexer could return null or throw an exception if you try to access a key you believe is already in the dictionary. Hence, not thread safe.
I missed the last fine-print of Ayende's entry. Ok, I'll agree that in a bad implementation, thread B while executing its outer ContainsKey(o) enumerating can potentially break.
Comment preview