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
2
votes
3 answers

Multiple thread access to a static object of a non-static class

By default non-static methods have their own instance of variables for each thread when accessed via multiple threads, thus rendering them thread safe if they do not include a public variable etc. On the other hand, variables in static methods are…
Chiao
  • 176
  • 2
  • 6
2
votes
2 answers

How do bulk actions affect a ConcurrentDictionary?

I'm confused about how Concurrent Dictionaries lock their resources. For example, if I run a method that iterates over every item in the Dictionary and edits its value in a thread and I try to read the value of a key from another thread: Will the…
Alex Coronas
  • 468
  • 1
  • 6
  • 21
2
votes
2 answers

Compare items in two lists and replace foreach loop with LINQ

I have two Lists: List list1; List list2; I have following code for compearing items in Lists: ConcurrentDictionary compareDictionary = new ConcurrentDictionary(); for (int i = 0; i < lis1.Count;…
2
votes
1 answer

ConcurrentDictionary: Proper small initial capacity

I keep running into a lack of guidance choosing proper initial capacities for ConcurrentDictionary. My general use case is those situations where you really want to do something like the following, but cannot: public static class…
Timo
  • 7,992
  • 4
  • 49
  • 67
2
votes
1 answer

How to implement TryRemove conditional to ConcurrentDictionary?

Recently I had a need for a Dictionary that I could update from multiple threads, and the obvious candidate for this job was the build-in ConcurrentDictionary. Unfortunately I ended up not using it, and using instead a normal Dictionary protected…
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
2
votes
1 answer

What element does ConcurrentDictionary.ElementAt return

In my code I got an ConcurrentDictionary now I want to iterate over each element in the Dictionary, but if a condition is true I want to remove an element from this Dictionary so I can't use a foreach loop. Also it might happen that the Dictionary…
Twenty
  • 5,234
  • 4
  • 32
  • 67
2
votes
1 answer

Get random element from concurrent dictionary c#

I'm looking for a data structure that is able to handle concurrency well (as removal & add from multiple threads will happen), and that allows me to map quickly (O(1)) from a hashed key to the corresponding object. I also have to be able to…
Cholesterol
  • 177
  • 2
  • 10
2
votes
2 answers

Is this thread safe with this ConcurrentDictionary and AddOrUpdate method?

I have a doubt with the concurrent dictionary in C#. In another question I was asked how to have a concurrent dictionary with a hashset as value, but it isn't a good idea to work with a hashset, it is better to use a concurrent dictionary as value.…
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
2
votes
1 answer

Can an .NET ConcurrentDictionary GetOrAdd method call an async method?

I'm looking at using a ConcurrentDictionary to hold some cached data, which comes from a slow source (eg. a database). The call to the database is async. So, is it possible to have the concurrent dictionary call an async method if the item doesn't…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
2
votes
1 answer

ConcurrentDictionary<(enum a, enum b), T>?

Found this in some api dode. It's not accepted by VS 2015 to have two enums as key, and I've never seen such declaration before. I assume it has worked for whom wrote it, so what should I learn..? private readonly ConcurrentDictionary<(Pair c,…
bretddog
  • 5,411
  • 11
  • 63
  • 111
2
votes
3 answers

C# Concurent dictionary - lock on value

I'm working on a service which is responsible for logging requests sent to our service. The service is working offline ( is being fired and forget ). We are saving the requests to different databases based on some input parameter(product id). We…
MajkeloDev
  • 1,661
  • 13
  • 30
2
votes
2 answers

How to implement Caching with data size limit?

I have multiple threads asking for data that have to be loaded over network. In order to have less network traffic and faster responses, I'd like to cache data, which are often requested. I also want to limit the Cache's data size. My class looks…
Ben
  • 4,486
  • 6
  • 33
  • 48
2
votes
1 answer

ConcurrentDictionary - Converting addValueFactory (Func ) into an updateValueFactory (Func)?

I am working with a method that, internally, calls ConcurrentDictionary.GetOrAdd(). I would like to add a call to ConcurrentDictionary.AddOrUpdate() insde the same method. However the containing method only receives an addValueFactory parameter…
Martin Hansen Lennox
  • 2,837
  • 2
  • 23
  • 64
2
votes
1 answer

Concurrent ToLookup() conversion?

How do I make ToLookup() concurrent? I have a some code like this: myRepository.GetAllContacts().ToLookup( c => c.COMPANY_ID); I would like to have a structure similar to this: new ConcurrentDicitonary>(); //…
Bill Software Engineer
  • 7,362
  • 23
  • 91
  • 174
2
votes
2 answers

Concurrent Dictionary AddOrUpdate not adding, updating and returning correctly

I am using concurrent dictionary addOrUpdate in C#. The problem is that this dictionary is not mainting the whole dictionary(list) instead of this it only addOrUpdate last record into the dictionary and that record found multiple times in…
Zia Ul Mustafa
  • 301
  • 3
  • 14