Questions tagged [concurrentdictionary]

ConcurrentDictionary is a .Net thread-safe collection of key-value pairs that can be accessed by multiple threads at the same time.

ConcurrentDictionary is a .Net thread-safe collection of key-value pairs that can be accessed by multiple threads at the same time.

References

331 questions
151
votes
7 answers

.NET - Dictionary locking vs. ConcurrentDictionary

I couldn't find enough information on ConcurrentDictionary types, so I thought I'd ask about it here. Currently, I use a Dictionary to hold all users that is accessed constantly by multiple threads (from a thread pool, so no exact amount of…
TheAJ
  • 10,485
  • 11
  • 38
  • 57
91
votes
5 answers

When should I use ConcurrentDictionary and Dictionary?

I'm always confused on which one of these to pick. As I see it I use Dictionary over List if I want two data types as a Key and Value so I can easily find a value by its key but I am always confused if I should use a ConcurrentDictionary or…
Ashkru
  • 1,495
  • 1
  • 17
  • 26
84
votes
3 answers

Can ConcurrentDictionary.TryAdd fail?

This is more of an academic question... but can ConcurrentDictionary.TryAdd fail? And if so in what cases and why?
Dave Lawrence
  • 3,843
  • 2
  • 21
  • 35
47
votes
5 answers

ConcurrentDictionary GetOrAdd async

I want to use something like GetOrAdd with a ConcurrentDictionary as a cache to a webservice. Is there an async version of this dictionary? GetOrAdd will be making a web request using HttpClient, so it would be nice if there was a version of this…
Zeus82
  • 6,065
  • 9
  • 53
  • 77
37
votes
6 answers

How can I convert a ConcurrentDictionary to a Dictionary?

I have a ConcurrentDictionary object that I would like to set to a Dictionary object. Casting between them is not allowed. So how do I do it?
umbersar
  • 1,821
  • 3
  • 24
  • 34
33
votes
2 answers

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

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…
ChaseMedallion
  • 20,860
  • 17
  • 88
  • 152
31
votes
7 answers

Tuple vs string as a Dictionary key in C#

I have a cache that I implement using a ConcurrentDictionary, The data that I need to keep depends on 5 parameters. So the Method to get it from the cache is: (I show only 3 parameters here for simplicity, and I changed the data type to represent…
Shahar
  • 655
  • 2
  • 7
  • 23
29
votes
2 answers

How to chose concurrency level of a concurrent dictionary

I'm using a concurrent dictionary to store about two million records and want to know what to initialize the concurrency level of my dictionary to. The MSDN page has the following comment in its example code: The higher the concurrencyLevel, the…
Michael Smale
  • 583
  • 9
  • 15
26
votes
9 answers

ConcurrentDictionary<> performance at a single thread misunderstanding?

Related brief info: AFAIK , The concurrent stack, queue, and bag classes are implemented internally with linked lists. And I know that there is much less contention because each thread is responsible for its own linked list. Any way , my question…
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
22
votes
2 answers

How do you set a value in a ConcurrentDictionary regardless of whether it contains the Key

First of all, is it safe to simply add an item to a concurrent dictionary using the indexed assignment (e.g. myConcurrentDictionary[someKey] = someValue;)? I'm just confused because it hides the IDictionary methods (e.g. Add). Why does AddOrUpdate()…
user2434174
20
votes
5 answers

How to initialize a ConcurrentDictionary? Error: "Cannot access private method 'Add' here"

I have a static class in which I am using dictionaries as lookup tables to map between .NET types and SQL types. Here is an example of such a dictionary: private static readonly Dictionary SqlServerMap = new Dictionary { …
Drew
  • 1,277
  • 3
  • 21
  • 39
20
votes
1 answer

ConcurrentDictionary TryGetValue vs []. Is [] still thread-safe?

I have the following ConcurrentDictionary: ConcurrentDictionary sessions; I know that sessions.TryGetValue(key, out session) is thread-safe, but my question is if sessions[key] is also thread-safe? sessions.TryGetValue(key, out…
crush
  • 16,713
  • 9
  • 59
  • 100
19
votes
6 answers

Caching asynchronous operations

I am looking for an elegant way of caching the results of my asynchronous operations. I first had a synchronous method like this: public String GetStuff(String url) { WebRequest request = WebRequest.Create(url); using (var response =…
vtortola
  • 34,709
  • 29
  • 161
  • 263
16
votes
2 answers

Calling ToList() on ConcurrentDictionary while adding items

I've run into an interesting issue. Knowing that the ConcurrentDictionary is safely enumerable while being modified, with the (in my case) unwanted side-effect of iterating over elements that may disappear or appear multiple times, I…
Roland Szakacs
  • 161
  • 1
  • 5
15
votes
5 answers

How to achieve remove_if functionality in .NET ConcurrentDictionary

I have a scenario where I have to keep reference counted object for given key in the ConcurrentDictionary, if reference count reaches 0, I want to delete the key. This has to be thread safe hence I am planning to use the ConcurrentDictionary. Sample…
1
2 3
22 23