I am using a concurrent dictionary as a thread-safe static cache and noticed the following behavior:
From the MSDN docs on GetOrAdd:
If you call GetOrAdd simultaneously on different threads, addValueFactory may be called multiple times, but its key/value pair might not be added to the dictionary for every call.
I would like to be able to guarantee that the factory is only called once. Is there any way to do this with the ConcurrentDictionary API without resorting to my own separate synchronization (e. g. locking inside valueFactory)?
My use case is that valueFactory is generating types inside a dynamic module so if two valueFactories for the same key run concurrently I hit:
System.ArgumentException: Duplicate type name within an assembly.
This is not uncommon with Non-Blocking Algorithms. They essentially test for a condition confirming there is no contention using
Interlock.CompareExchange
. They loop around though until the CAS succeeds. Have a look atConcurrentQueue
page (4) as a good intro to Non-Blocking AlgorithmsShort answer is no, it's the nature of the beast that it will require multiple attempts to add to the collection under contention. Other than using the other overload of passing a value, you'd need to protect against multiple calls inside your value factory, perhaps using a double lock / memory barrier.
You could use a dictionary that is typed like this:
ConcurrentDictionary<TKey, Lazy<TValue>>
, and then the your value factory would return aLazy<TValue>
object that has been initialized withLazyThreadSafetyMode.ExecutionAndPublication
, which is the default option used byLazy<TValue>
if you don't specify it. By specifying theLazyThreadSafetyMode.ExecutionAndPublication
you are telling Lazy only one thread may initialize and set the value of the object.This results in the
ConcurrentDictionary
only using one instance of theLazy<TValue>
object, and theLazy<TValue>
object protects more than one thread from initializing its value.i.e.
The downside then is you'll need to call *.Value every time you are accessing an object in the dictionary. Here are some extensions that'll help with that.