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
3
votes
1 answer

Faster KV vs dictionary vs concurrent dictionary which one to use when only updating value of dictionary

Streaming live prices for stocks, each stock is key (int) and its value type is struct (not class) Dictionary. This dictionary is initialized once with all keys, and updating (no new additions) values every millisecond randomly from…
3
votes
1 answer

Why ConcurrentDictionary TryUpdate() method requires indicating oldValue?

When calling TryUpdate you should specify old value besides key, why is it required ? And additional question why TryUpdate method (and others similar) have while(true) loop wrapper inside ?
Ddd
  • 113
  • 2
  • 15
3
votes
2 answers

ConcurrentDictionary performance

I am struggling with this problem and would really appreciate any help. I'm working on an existing project. I have added logic that counts values combinations, and makes sure we do not pass some limit. For example, given this data table…
Nika
  • 57
  • 7
3
votes
3 answers

Is it possible to miss an initial item when enumerating ConcurrentDictionary?

I'm enumerating ConcurrentDictionary, I need to be sure I don't miss any initial item. In other words, I need to be sure I enumerate all initial items. Initial items: all items in dictionary when the enumeration starts. The documentation says: The…
Jesús López
  • 8,338
  • 7
  • 40
  • 66
3
votes
2 answers

How long does the ConcurrentDictionary object last?

I am using MVC 5, ASP.NET 4.7 on Azure App Services I am using the ConcurrentDictionary object to persist data to save multiple calls to the data source. A few questions on its behaviour: 1) Once it has been populated, then it will persist for…
SamJolly
  • 6,347
  • 13
  • 59
  • 125
3
votes
0 answers

ConcurrentDictionary adding and reading data

I was just testing how ConcurrentDictionary works. We know that it is thread safe. So I create two tasks and run both task simultaneously. first task adds data to ConcurrentDictionary and the second task reads data from it. The output I got was bit…
Monojit Sarkar
  • 2,353
  • 8
  • 43
  • 94
3
votes
2 answers

How to add a new element to a hashset that is value of a ConcurrentDictionary?

I have a ConcurrentDictionary that has as key a long and as value a hashset of int. I want that if the key isn't in the dictionary, add a new hashset with the first element. If the key exists, add the new element to the existing dictionary. I am…
Álvaro García
  • 18,114
  • 30
  • 102
  • 193
3
votes
2 answers

GetOrAdd new vs factory performance

Which of the following two pieces of code will perform better in different cases and why? 1. private readonly ConcurrentDictionary> _coll; _coll.GetOrAdd(1, new List()); This creates a new List on every call even when it is not…
Hele
  • 1,558
  • 4
  • 23
  • 39
3
votes
1 answer

C# ConcurrentDictionary usage inconsistent accessibility?

I'm following along with a tutorial to build a chat client and server, right now I've got the following error: Inconsistent accessibility: field type 'System.Collections.Concurrent.ConcurrentDictionary' is less…
3
votes
3 answers

Thread safe re-initialization of concurrent dictionary

I want to know if the following code is thread safe, which I assume it is not. And how I could possibly make it thread safe? Basically I have a ConcurrentDictionary which acts as a cache for a database table. I want to query the DB every 10 seconds…
Zapnologica
  • 22,170
  • 44
  • 158
  • 253
3
votes
2 answers

Waiting for all threads to finish writing in ConcurrentDictionary

There's a concurrent dictionary, which is collecting information from different sources and once in a minute is supposed to get refreshed and pass collected data to another handler. var currentDictionarySnapshot =…
Sovent
  • 119
  • 2
  • 13
3
votes
2 answers

What could solves this multi-threaded scenario better than Concurrent collections

I have a persistent B+tree, multiple threads are reading different chunks of the tree and performing some operations on read data. Interesting part: each thread produces a set of results, and as end user I want to see all the results in one place.…
3
votes
1 answer

Safely removing list mapping from ConcurrentDictionary

I have a ConcurrentDictionary which maps a simple type to a list: var dict = new ConcurrentDictionary>(); I can use AddOrUpdate() to cater for both initialization of the list when the first value is added, and addition of…
Gigi
  • 28,163
  • 29
  • 106
  • 188
3
votes
2 answers

ConcurrentDictionary.GetOrAdd when valueFactory has side-effects

I'm trying to offload work from my database server by introducing a cache layer for some very central functions that insert a value to a table in the database and retrieves the id. This is in a multi-threaded environment. My first approach…
3
votes
2 answers

How can i Add or Update this .NET collection, during a Parallel.ForEach?

I have a list of files, where each file contains a list of Foo data. Now, the same piece of Foo data (eg. Id = 1) might exist in multiple files, but the more recent piece of data would overwrite an existing one. I'm just reading each piece of data…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647