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

Lock for ConcurrentDictionary when AddOrUpdate-ing?

I use a ConcurrentDictioanry> to access some data across many threads. I read in this article (scroll down) that the method AddOrUpdate is not executed in the lock, so it could endanger thread-safety. My code is as…
Paul
  • 20,883
  • 7
  • 57
  • 74
5
votes
2 answers

ConcurrentDictionary - broken dictionary or bad code?

Alright, so I'm running into a strange little issue and frankly I'm out of ideas. I wanted to throw this out there to see if I'm missing something that I've done wrong, or if ConcurrentDictionary isn't working correctly. Here's the code: (Cache is…
5
votes
1 answer

How does this ConcurrentDictionary + Lazy> code work?

There's various posts/answers that say that the .NET/.NET Core's ConcurrentDictionary GetOrAdd method is not thread-safe when the Func delegate is used to calculate the value to insert into the dictionary, if the key didn't already exist. I'm under…
Pure.Krome
  • 84,693
  • 113
  • 396
  • 647
5
votes
1 answer

Is it safe to copy a ConcurrentDictionary to a normal Dictionary, by enumerating it directly, based on the current implementation of the class?

TL;DR: Is it possible for a single enumeration of a ConcurrentDictionary, to emit the same key twice? Does the current implementation of the ConcurrentDictionary class (.NET 5) allow this possibility? I have a ConcurrentDictionary
Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
5
votes
2 answers

C# Dictionary concurrent add or modify only for different keys, is ConcurrentDictionary necessary?

I have a dictionary only supports add and modify operations and can be concurrently operated, but always for different keys. Keys are int and values are a reference type. Also modify means change some properties of a value. My questions are: Do I…
Student222
  • 3,021
  • 2
  • 19
  • 24
5
votes
3 answers

TryRemove a key-value pair from ConcurrentDictionary in C#

The scenario I have is I want a method on ConcurrentDictionary like this. bool TryRemove(TKey key, TValue value) { // remove the value IF the value passed in == dictionary[key] // return false if the key is not in the dictionary, or the…
Frank Bryce
  • 8,076
  • 4
  • 38
  • 56
5
votes
0 answers

Using ConcurrentDictionary in a PCL from a UWP app

I recently had a problem and would like some explanation of why : In a Portable Class Library (PCL) using Profile5 (.Net 4, Windows 8) I have this code : using System.Collections.Concurrent; public class Foo { public static void…
Julien Roncaglia
  • 17,397
  • 4
  • 57
  • 75
5
votes
2 answers

call valuefactory in concurrent dictionary in async way

I am new to C#'s ConcurrentDictionary class, and I wonder how I can use a valueFactory in the GetOrAdd method in an async way. public class Class1 { public int X = 10; public Class1(int x) { X = x; …
Sean
  • 981
  • 1
  • 9
  • 19
5
votes
2 answers

Volatile for structs and collections of structs

I would like to use net wisdom to clarify some moments regarding multi-threading in .net. There are a lot of stuff in the internet about it however I was not able to find a good answer to my question. Let say we want to maintain a state of…
irriss
  • 742
  • 2
  • 11
  • 22
5
votes
2 answers

Different behaviour when collection modified between Dictionary and ConcurrentDictionary

With a normal Dictionary code as list below, I get exception that Collection was modified; enumeration operation may not execute. Dictionary dict2 = new Dictionary(); dict2.Add(1, 10); dict2.Add(2, 20); dict2.Add(3,…
whoami
  • 1,689
  • 3
  • 22
  • 45
5
votes
2 answers

Is the list order of a ConcurrentDictionary guaranteed?

I am using a ConcurrentDictionary to store log-lines, and when I need to display them to the user I call ToList() to generate a list. But the weird thing is that some users receive the most recent lines first in the list, while they should logically…
Maestro
  • 9,046
  • 15
  • 83
  • 116
5
votes
2 answers

Create a Dictionary using Linq

How can I create a Dictionary (or even better, a ConcurrentDictionary) using Linq? For example, if I have the following XML
Baruch
  • 20,590
  • 28
  • 126
  • 201
4
votes
2 answers

Looping through ConcurrentDictionary while modifying values

I have a static ConcurrentDictionary in a static class. In the static constructor of the class, I call a private method via Task.Run to indefinitely loop through the dictionary and remove items that have expired, i.e. a significant amount of time…
globetrotter
  • 997
  • 1
  • 16
  • 42
4
votes
2 answers

How to implement tryAdd multiple entries into concurrent dictionary?

I have a concurrent dictionary which I am using it to do some thread-safe functionalities. For example I have the following code : var myConcurrenctDictionary= new ConcurrentDictionary(); if (myConcurrenctDictionary.TryAdd(personId,…
Simple Code
  • 2,354
  • 2
  • 27
  • 56
4
votes
1 answer

Is it thread safe to access ConcurrentDictionary values after getting these values with Linq

I have a ConcurrentDictionary like this: ConcurrentDictionary concurrentDictionary = new ConcurrentDictionary(); This is readable and writable dictionary that can be used by many threads. I can manage writable side in a thread…
rodins
  • 306
  • 2
  • 11