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

Use LINQ ToDictionary with ConcurrentDictionary

I'm using a dictionary to collect events in a multithread application, using lock when I add an event and not using it when I search for one. Every hour or so I run a cleanup of the events older than a certain time. Very simple and it works. I'd…
Mattia Durli
  • 757
  • 7
  • 19
3
votes
5 answers

How should I "Cancel" an AddOrUpdate within ConcurrentDictionary?

I've read the MSDN documents and this blog and I need the following logic: For a ConcurrentDictionary If the string doesn't exist, add it,and make sure I set the bool to True while adding If the string does exist, only change the bool…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
3
votes
1 answer

Whats the risk of using TPL with ConcurrentDictionary with "addValueFactory"? MSDN implies threading issues

MSDN says the following about addValueFactory in a multi threaded environment: Remarks If you call AddOrUpdate simultaneously on different threads, addValueFactory may be called multiple times, but its key/value pair might not be added to the…
makerofthings7
  • 60,103
  • 53
  • 215
  • 448
3
votes
2 answers

A Better Way To Make My Parallel.ForEach Thread Safe?

I would like to make the following code thread-safe. Unfortunately, I have tried locking at various levels within this code with no success. The only instance I can seem to achieve thread-safety is to place a lock around the entire loop which…
3
votes
4 answers

Getting a value from a ConcurrentDictionary

If I have this ConcurrentDictionary: public class User { public string Context { get; set; } public bool Owner { get; set; } } protected static ConcurrentDictionary OnlineUsers = new ConcurrentDictionary(); Does…
Joey Morani
  • 25,431
  • 32
  • 84
  • 131
3
votes
3 answers

Extension methods on System.Collections.Concurrent collections are thread-safe?

There are several extension methods for example, on the ConcurrentDictionary class, because it implements the IEnumerable interface. Are these methods (for example First, Sum, Take, etc.) are inherently thread-safe or not?
Zsolt
  • 3,263
  • 3
  • 33
  • 48
2
votes
1 answer

Where is ConcurrentDictionary in Reactive Extensions .NET 3.5

My questions is simple. After heavy googling I have learned that I can use ConcurrentDictionary in .NET 3.5 projects using Reactive Extensions and System.Threading.dll version from its install directory. First of all there is no…
Tobiasz
  • 1,059
  • 1
  • 12
  • 29
2
votes
1 answer

Is there a way to identify whether a value was successfully found or if it was added when using ConcurrentDictionary.GetOrAdd?

I have some code that uses the GetOrAdd function from ConcurrentDictionary to add values with a unique key to a holder. However, I want to be able to tell if a values key is found (i.e. a true returned from TryGet) or whether it has to be added. I…
Sam
  • 21
  • 1
2
votes
1 answer

Avoid consumers processing the same entry

Cant prevent multiple consumer from processing the same record in queue using System.Threading.Channels library, when writer enqueues some model, one of multiple consumers starts to process it. during this, writer goes to the database and reads the…
2
votes
2 answers

Migrating from Dictionary to ConcurrentDictionary, what are the common traps that I should be aware of?

I am looking at migrating from Dictionary to ConcurrentDictionary for a multi thread environment. Specific to my use case, a kvp would typically be > What do I need to look out for? How do I implement successfully for thread…
Pippo
  • 1,439
  • 1
  • 18
  • 35
2
votes
1 answer

How is a null value getting into my concurrent dictionary?

I have a simple connection manager service in my web app that keeps track of websocket connections against a user GUID they are associated with. These are stored in a dictionary with the key being a user GUID and the value being a list of connection…
Jacob
  • 67
  • 1
  • 5
2
votes
2 answers

Concurrent dictionary best way to remove Collection type values

I have many clients where each client is having multiple simultaneous tunnels (data fetching middleware). I have to manage all the clients with live tunnels of each of the client. My Tunnel class is having many properties and functions, I am showing…
2
votes
2 answers

Is it OK to use Dictionary instead of ConcurrentDictionary in multithreading program when the number of keys is fixed?

I know in multithreading program, we need to use ConcurrentDictionary, ConcurrentBag etc those thread-safe collection. But in my situation, the number of keys in Dictionary is fixed, I have exact 5 keys which I already know before the program…
user16276760
2
votes
2 answers

What's the reason for the comparisonValue argument in the ConcurrentDictionary.TryUpdate method?

Why do we need third argument comparisonValue in ConcurrentDictionary.TryUpdate method? And why will updating not succeed if already existed value is not equal to comparisonValue? Can't we just replace existed value with the new one just like in…
Wachburn
  • 2,842
  • 5
  • 36
  • 59
2
votes
1 answer

Is this reliable way to use AddOrUpdate on ConcurrentDictionary

I've been following Pluralsight course, authored by Simon Robinson on Concurrent Collections. He uses AddOrUpdate in the following way in order to make it thread-safe: public bool TrySellShirt(string code) { bool success = false; …
Sha Kal
  • 199
  • 1
  • 1
  • 6