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
14
votes
4 answers

How do you convert a dictionary to a ConcurrentDictionary?

I have seen how to convert a ConcurrentDictionary to a Dictionary, but I have a dictionary and would like to convert to a ConcurrentDictionary. How do I do that?... better yet, can i set the link statement to be a ConcurrentDictionary? var…
MrM
  • 21,709
  • 30
  • 113
  • 139
13
votes
1 answer

Is C# LINQ OrderBy threadsafe when used with ConcurrentDictionary?

My working assumption is that LINQ is thread-safe when used with the System.Collections.Concurrent collections (including ConcurrentDictionary). (Other Overflow posts seem to agree: link) However, an inspection of the implementation of the LINQ…
12
votes
1 answer

.NET ConcurrentDictionary initial capacity set to arbitrary prime number rather than expected capacity in MSDN example documentation. Why?

I was just looking at the MSDN documentation for ConcurrentDictionary, and I saw this in the "example" code: // We know how many items we want to insert into the ConcurrentDictionary. // So set the initial capacity to some prime number above that,…
Matthew King
  • 5,114
  • 4
  • 36
  • 50
12
votes
3 answers

Which members of .NET's ConcurrentDictionary are thread-safe?

The MSDN documentation of System.Collections.Concurrent.ConcurrentDictionary says: Thread Safety All public and protected members of ConcurrentDictionary are thread-safe and may be used concurrently from multiple threads. However,…
Peter
  • 3,322
  • 3
  • 27
  • 41
12
votes
6 answers

Thread safe Dictionary-like collection with upper bound

I am after a collection with the following properties: Thread safe: It will be used in asp.net and multiple clients could try to add, remove and access members concurrently Max elements: I want to be able to set an upper bound, a maximum number of…
12
votes
2 answers

Benefits of Redis over c# Dictionary

I am wondering what the benefits of Redis with its C# client over Dictionary/ConcurrentDictionary and otherwise. I am not sure when using redis is considered overkill for a dictionary storage. Thanks.
Aviran Cohen
  • 5,581
  • 4
  • 48
  • 75
11
votes
2 answers

How can I tell `ConcurrentDictionary.GetOrAdd` to not add a value?

I have several cases where I use ConcurrentDictionary for caching of values, but often times I need to perform validation of the value to decide whether to add it to the cache using ConcurrentDictionary.GetOrAdd(TKey,…
M.Babcock
  • 18,753
  • 6
  • 54
  • 84
11
votes
3 answers

Can ConcurrentDictionary.GetOrAdd() be called recursively?

ConcurrentDictionary.GetOrAdd(TKey, Func) accepts a factory function to allow lazy instantiation of the item to be put into the dictionary. Is it safe to define a factory function that itself calls GetOrAdd(), i.e. GetOrAdd is being…
redcalx
  • 8,177
  • 4
  • 56
  • 105
11
votes
3 answers

ConcurrentDictionary.GetOrAdd - Add only if not null

I'm using ConcurrentDictionary to cache data with parallel access and sometimes new items can be stored in db and they are not loaded into cache. This is reason why I use GetOrAdd public User GetUser(int userId) { return _user.GetOrAdd(userId,…
y0j0
  • 3,369
  • 5
  • 31
  • 52
11
votes
1 answer

.NET ConcurrentDictionary.ToArray() ArgumentException

Sometimes I get the error below when I call ConcurrentDictionary.ToArray. Error Below: System.ArgumentException: The index is equal to or greater than the length of the array, or the number of elements in the dictionary is greater than the…
Kess
  • 488
  • 3
  • 11
11
votes
1 answer

Is ConcurrentDictionary ContainsKey method synched?

Simple question Assume that i have a ConcurrentDictionary I use TryAdd and ContainsKey methods Now assume that from 100 threads i started to process stuff. Assume that when 3 threads while adding a new key with TryAdd method another 3 threads…
Furkan Gözükara
  • 22,964
  • 77
  • 205
  • 342
11
votes
3 answers

Need an efficient in-memory cache that can process 4k to 7k lookups or writes per second

I have an efficient C# application that receives 80 bytes of data at a rate of 5k to 10k records per second on a multi threaded CPU. I need to now set up a in memory-cache to detect and filter duplicate records so I can suppress them from…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
11
votes
2 answers

some questions around the use of ConcurrentDictionary

I am currently writing a C# application. I am new to using a ConcurrentDictionary so have some questions around its thread safety. Firstly, this is my dictionary: /// /// A dictionary of all the tasks scheduled ///
amateur
  • 43,371
  • 65
  • 192
  • 320
10
votes
1 answer

ConcurrentDictionary + Lazy -- would instantiation happen only once?

Scenario Let's say we have: var dictionary = new ConcurrentDictionary>(); Instantiating Heavy is very resource-consuming. Let's consider this code: return dictionary.GetOrAdd("key", key => { return new Lazy(() => …
ebvtrnog
  • 4,167
  • 4
  • 31
  • 59
9
votes
3 answers

While updating a value in concurrent dictionary is better to lock dictionary or value

I am performing two updates on a value I get from TryGet I would like to know that which of these is better? Option 1: Locking only out value? if (HubMemory.AppUsers.TryGetValue(ConID, out OnlineInfo onlineinfo)) { lock (onlineinfo) { …
Danial Ahmed
  • 856
  • 1
  • 10
  • 26
1
2
3
22 23