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

ConcurrentDictionary's optimistically concurrent Remove method

I was looking for a method in ConcurrentDictionary that allows me to remove an entry by key, if and only if the value is equal to one that I specify, something like the equivalent of TryUpdate, but for removals. The only method that does this seems…
Evgeniy Berezovsky
  • 18,571
  • 13
  • 82
  • 156
8
votes
2 answers

Is HashSet thread safe as a value of ConcurrentDictionary>?

If I have the following code: var dictionary = new ConcurrentDictionary>(); foreach (var user in users) { if (!dictionary.ContainsKey(user.GroupId)) { dictionary.TryAdd(user.GroupId, new HashSet()); } …
Marko
  • 12,543
  • 10
  • 48
  • 58
8
votes
1 answer

atomic addorupdate (trying to write named locker using concurrent dictionary)

ConcurrentDictionary Pitfall - Are delegates factories from GetOrAdd and AddOrUpdate synchronized? notes that AddOrUpdate is not atomic (and can't guarantee delegates won't be run more than once). I'm trying to implement a name locking…
TravisK
  • 129
  • 8
8
votes
2 answers

getting argument exception in concurrent dictionary when sorting and displaying as it is being updated

I am getting a hard to reproduce error in the following program in which a number of threads update a concurrent dictionary in parallel and the main thread displays the state of the dictionary in sorted order after fixed time intervals, until all…
8
votes
3 answers

How to wrap ConcurrentDictionary in BlockingCollection?

I try to implement a ConcurrentDictionary by wrapping it in a BlockingCollection but did not seem to be successful. I understand that one variable declarations work with BlockingCollection such as ConcurrentBag, ConcurrentQueue, etc. So, to…
7
votes
2 answers

Stop Reentrancy on MemoryCache Calls

The app needs to load data and cache it for a period of time. I would expect that if multiple parts of the app want to access the same cache key at the same time, the cache should be smart enough to only load the data once and return the result of…
Christian Findlay
  • 6,770
  • 5
  • 51
  • 103
7
votes
1 answer

Why ConcurrentDictionary has AddOrUpdate and GetOrAdd, but Dictionary has not?

In the .NET Framework, there is Dictionary and ConcurrentDictionary. These provide method like Add, Remove, and so on... I know when we design a multi-thread program, we use ConcurrentDictionary to replace Dictionary for thread-safety. I wonder why…
Po-Sen Huang
  • 445
  • 6
  • 24
7
votes
1 answer

How to get moment-in-time snapshot of ConcurrentDictionary?

MSDN states that the enumerator returned from the ConcurrentDictionary does not represent a moment-in-time snapshot of ConcurrentDictionary. Although it will be rarely needed in multi-threaded environment, but if one wants, what is the best way to…
Ramy
  • 305
  • 1
  • 5
  • 10
7
votes
1 answer

ConcurrentDictionary AddOrUpdate a list

I'm trying to use a ConcurrentDictionary to help with a filtering task. If a number appears in list, then I want to copy an entry from one dictionary to another. But this part of the AddOrUpdate is not right - v.Add(number) I get "Cannot…
Bryan
  • 5,065
  • 10
  • 51
  • 68
7
votes
1 answer

Can Bounded BlockingCollections Lose Data During Adds

I have a BlockingCollection(ConcurrentBag, 50000) where I am trying to use a very small Bounded Capacity of 50,000 for the producer threads in order to maximize the number of records I can process in my consumer thread's ConcurrentDictionary. The…
7
votes
6 answers

Cast from ConcurrentDictionary to IDictionary

Does a cast from ConcurrentDictionary to IDictionary cut off the thread-safe implementation, since IDictionary doesn't have GetOrAdd and AddOrUpdate methods ?
Luciano
  • 2,695
  • 6
  • 38
  • 53
6
votes
2 answers

ConcurrentDictionary and Clear()-function. Making values export threadsafe without data-loss

Any ideas of making ConcurrentDictionary threadsafe in condition where values are exported to list ex, and after that dictionary is cleared. So that any other thread cannot add data between exporting and clearing. Like this: " List list; list…
6
votes
2 answers

How to atomic update a value in the ConcurrentDictionary by key only if it exists

ConcurrentDictionary.TryUpdate method requires the comparisonValue that is compared with the value of the element that has the specified key. But if I trying to do something like this: if (!_store.TryGetValue(book.Id, out Book existing)) { throw…
user10485375
6
votes
1 answer

How can I make this Concurrent Dictionary expire with a timer?

This code seems to do a good job of caching async method results. I would like to add some sort of expiration to it. I have tried Tuple but I was not successful in getting it to fully work / compile. private static readonly…
jxmiller
  • 78
  • 2
  • 8
6
votes
2 answers

ConcurrentDictionary is it threadsafe to edit the value after a GetOrAdd?

I am using the GetOrAdd method of the concurrent dictionary to retrieve a list of values then with a reference to that list of values I'm editing them. Is it thread-safe to do it this way? The first method I'm adding a value and the second method…
1 2
3
22 23