Ayende @ Rahien

My name is Oren Eini
Founder of Hibernating Rhinos LTD and RavenDB.
You can reach me by phone or email:


+972 52-548-6969

, @ Q c

Posts: 6,128 | Comments: 45,550

filter by tags archive

Why is this not thread safe?

time to read 2 min | 238 words

Originally posted at 4/15/2011

Take a look at the following piece of code:

if(items.ContainsKey(key) == false)
        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.



For example during an array expansion.

john wingfield

so what's the solution? could you use a resetEvent to only block when writing?

Alex Simkin

Just inner part works fine:



    if(items.ContainsKey(key) == false)


        items.Add(key, val);


Jon Wingfield

@Alex, that's the most pessimistic approach though.

Eber Irigoyen

So this code is thread safe, the code that would not be thread safe would be the one enumerating the collection

Erik Juhlin

@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

Sony Mathew

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.

Harry Steinhilber


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.

Sony Mathew

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

Comments have been closed on this topic.


  1. The worker pattern - 14 hours from now

There are posts all the way to May 30, 2016


  1. The design of RavenDB 4.0 (14):
    26 May 2016 - The client side
  2. RavenDB 3.5 whirl wind tour (14):
    25 May 2016 - Got anything to declare, ya smuggler?
  3. Tasks for the new comer (2):
    15 Apr 2016 - Quartz.NET with RavenDB
  4. Code through the looking glass (5):
    18 Mar 2016 - And a linear search to rule them
  5. Find the bug (8):
    29 Feb 2016 - When you can't rely on your own identity
View all series


Main feed Feed Stats
Comments feed   Comments Feed Stats