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

How to improve performance of ConcurrentDictionary.Count in C#

Recently, I needed to choose between using SortedDictionary and SortedList, and settled on SortedList. However, now I discovered that my C# program is slowing to a crawl when performing SortedList.Count, which I check using a function/method called…
4
votes
5 answers

Why do ConcurrentQueue and ConcurrentDictionary have "Try" methods - TryAdd, TryDequeue - instead of Add and Dequeue?

ConcurrentQueue has TryDequeue method. Queue has just Dequeue method. In ConcurrentDictionary there is no Add method, but we have TryAdd instead. My question is: What is the diffrence between these concurrent collection methods? Why they are…
Kamil
  • 13,363
  • 24
  • 88
  • 183
4
votes
1 answer

Removing a key/value pair from a ConcurrentDictionary given a value

I have a concurrent dictionary with Ids as keys and tokens as values. There are instances where I will have an Id for which I want to remove tokens, and there are instances where I will have a specific token to remove. What could I call on the…
paulina
  • 199
  • 3
  • 14
4
votes
2 answers

Extreme Thread Safe Collection

I have a ConcurrentBag in .Net 4.5 which I am storing about 4,000 rows from a database. I'm storing the DTOs. My entire application relies on this. I have functions that return the entire list, and also have functions that return a single item. So…
bladefist
  • 1,084
  • 3
  • 15
  • 25
4
votes
1 answer

Lock ConcurrentDictionary while removing and adding multiple items?

I have read the following articles on StackOverflow: ConcurrentBag - Add Multiple Items? and Concurrent Dictionary Correct Usage but the answers are still somehow not obvious to me. I have this scenario: I have Leaderboard table in the database and…
Luke Vo
  • 17,859
  • 21
  • 105
  • 181
4
votes
1 answer

ConcurrentDictionary Lazy AddOrUpdate

I found this Extension for C# to convert GetOrAdd to Lazy and I want to do the same for AddOrUpdate. Can someone help me convert this to AddOrUpdate? public static class ConcurrentDictionaryExtensions { public static TValue LazyGetOrAdd
Andre DeMattia
  • 631
  • 9
  • 23
4
votes
3 answers

Is ConcurrentDictionary.Count > 0 the same as ConcurrentDictionary.Any()?

If I have a ConcurrentDictionary instance, does it matter whether I use the Count property or LINQ's Any()? I'd rather write dict.Any() instead of dict.Count > 0 as I think Any() is more descriptive. I'm only concerned about correctness, not…
Ðаn
  • 10,934
  • 11
  • 59
  • 95
4
votes
2 answers

Guarantees on documented/implicitly documented/undocumented behavior

Looking at the ConcurrentDictionary documentation it says the following: Represents a thread-safe collection of key/value pairs that can be accessed by multiple threads concurrently. Now when reading this it makes me think that I can call any…
Cheetah
  • 13,785
  • 31
  • 106
  • 190
4
votes
2 answers

ConcurrentDictionary & atomic operations- sometimes Lock is needed?

Let's say I have this simple code : (simplification) Where MyConCurrentDictionary is a static ConcurrentDictionary ( which resides in a different class). /*1*/ public void Send(string Message, string UserName) /*2*/ { …
Royi Namir
  • 144,742
  • 138
  • 468
  • 792
4
votes
3 answers

ConcurrentDictionary's GetOrAdd is not atomic. Any alternatives besides locking?

I'm using concurrent dictionary to hold open files. To open a new file, I do this: myDictionary.GetOrAdd (fName, (fn) => new StreamWriter(fn, true)); And with this, I regularly get following exception: System.IO.IOException: The process cannot…
Arsen Zahray
  • 24,367
  • 48
  • 131
  • 224
4
votes
2 answers

Why do I need to cast from ConcurrentDictionary to IDictionary in order to use Add()?

If I have a ConcurrentDictionary and want to use the Add() function, I need to cast to IDictionary: var cd = new ConcurrentDictionary(); cd.Add(1, 1); // Compile error: does not contain a definition for 'Add' IDictionary id =…
Jonathan
  • 6,939
  • 4
  • 44
  • 61
4
votes
2 answers

Lock On Value of ConcurrentDictionary

Partition is a small class I created. I have thousands of partitions that reside in a ConcurrentDictionary named Partitions. Before serialization, I want to lock a specific partition, do some work and then unlock the partition. During this time no…
Jake Drew
  • 2,230
  • 23
  • 29
4
votes
1 answer

Defending against race conditions in System.Collections.Concurrent.ConcurrentDictionary

The .NET ConcurrentDictionary is susceptible to a race condition that may cause unexpected data as explained at the bottom of this MSDN article. I'm assuming that there are several factors to take into account. Q: How should I write code that is…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
3
votes
2 answers

ConcurrentDictionary AddOrUpdate by predicate

I have a ConcurrentDictionary. I use its AddOrUpdate method to manipulate its items. My question is: is it possible to use AddOrUpdate's update parameter to contain an if statement? E.g. my ConcurrentDictionary contains objects that has string Id…
Tom
  • 3,899
  • 22
  • 78
  • 137
3
votes
2 answers

Parallel.ForEach and ConcurrentDictionary are slower than regular Dictionary

There is a task. The array contains arbitrary strings. We need to count how many times each of the strings occurs in the array. Solve the task in one thread and multithreaded, compare the execution time. For some reason, the single-threaded version…