How to chose concurrency level of a concurrent dictionary

The most important thing to understand is that even if you have more concurrent accesses than the concurrencyLevel, operations will still be thread-safe. That is, setting concurrencyLevel is a matter of performance, not correctness. concurrencyLevel specifies the number of independent locks which are available for map operations. Each lock is associated with a different subset … Read more

Why does ConcurrentDictionary.GetOrAdd(key, valueFactory) allow the valueFactory to be invoked twice?

You could use a dictionary that is typed like this: ConcurrentDictionary<TKey, Lazy<TValue>>, and then the your value factory would return a Lazy<TValue> object that has been initialized with LazyThreadSafetyMode.ExecutionAndPublication, which is the default option used by Lazy<TValue> if you don’t specify it. By specifying the LazyThreadSafetyMode.ExecutionAndPublication you are telling Lazy only one thread may initialize … Read more

How can I convert a ConcurrentDictionary to a Dictionary?

The ConcurrentDictionary<K,V> class implements the IDictionary<K,V> interface, which should be enough for most requirements. But if you really need a concrete Dictionary<K,V>… var newDictionary = yourConcurrentDictionary.ToDictionary(kvp => kvp.Key, kvp => kvp.Value, yourConcurrentDictionary.Comparer); // or… // substitute your actual key and value types in place of TKey and TValue var newDictionary = new Dictionary<TKey, TValue>(yourConcurrentDictionary, yourConcurrentDictionary.Comparer);

ConcurrentDictionary GetOrAdd async

GetOrAdd won’t become an asynchronous operation because accessing the value of a dictionary isn’t a long running operation. What you can do however is simply store tasks in the dictionary, rather than the materialized result. Anyone needing the results can then await that task. However, you also need to ensure that the operation is only … Read more

Can ConcurrentDictionary.TryAdd fail?

Yes it can, here are the conditions (from msdn): ArgumentNullException – when the key is null reference OverflowException – when max number of elements was reached It returns false if an element with the same key already exist Just to reiterate, this is nothing to do with concurrency. If you worry about two threads inserting … Read more

When should I use ConcurrentDictionary and Dictionary?

“Use ConcurrentDictionary if you use your dictionary in a lot in code” is kind of vague advice. I don’t blame you for the confusion. ConcurrentDictionary is primarily for use in an environment where you’re updating the dictionary from multiple threads (or async tasks). You can use a standard Dictionary from as much code as you … Read more

.NET – Dictionary locking vs. ConcurrentDictionary

A thread-safe collection vs. a non-threadsafe-collection can be looked upon in a different way. Consider a store with no clerk, except at checkout. You have a ton of problems if people don’t act responsibly. For instance, let’s say a customer takes a can from a pyramid-can while a clerk is currently building the pyramid, all … Read more

tech