Questions tagged [concurrent-collections]

Concurrent Collections are several collection implementations in .Net that support multi-threaded access in a safe and efficient way

Concurrent Collections are several collection implementations in .Net that support multi-threaded access in a safe and efficient way. Some of those collections are ConcurrentQueue, ConcurrentStack, ConcurrentBag.

References

82 questions
118
votes
4 answers

What to add for the update portion in ConcurrentDictionary AddOrUpdate

I am trying to re-write some code using Dictionary to use ConcurrentDictionary. I have reviewed some examples but I am still having trouble implementing the AddOrUpdate function. This is the original code: dynamic a = HttpContext; …
user438331
  • 1,337
  • 3
  • 10
  • 10
60
votes
1 answer

What is the difference between SynchronizedCollection and the other concurrent collections?

How does SynchronizedCollection and the concurrent collections in the System.Collections.Concurrent namespace differ from each other, apart from Concurrent Collections being a namespace and SynchronizedCollection being a…
Batrickparry
  • 1,329
  • 2
  • 13
  • 14
47
votes
5 answers

ConcurrentBag - Add Multiple Items?

Is there a way to add multiple items to ConcurrentBag all at once, instead of one at a time? I don't see an AddRange() method on ConcurrentBag, but there is a Concat(). However, that's not working for me: ConcurrentBag objectList = new…
Bob Horn
  • 30,755
  • 28
  • 102
  • 197
42
votes
11 answers

Why is ConcurrentBag so slow in .Net (4.0)? Am I doing it wrong?

Before I started a project, I wrote a simple test to compare the performance of ConcurrentBag from (System.Collections.Concurrent) relative to locking & lists. I am extremely surprised that ConcurrentBag is over 10 times slower than locking with a…
Tachy
  • 891
  • 1
  • 6
  • 13
30
votes
3 answers

Concurrent collection supporting removal of a specified item?

Quite simple: Other than ConcurrentDictionary (which I'll use if I have to but it's not really the correct concept), is there any Concurrent collection (IProducerConsumer implementation) that supports removal of specific items based on simple…
KeithS
  • 65,745
  • 16
  • 102
  • 161
29
votes
5 answers

ConcurrentDictionary vs Dictionary

As MSDN says ConcurrentDictionary Class Represents a thread-safe collection of key-value pairs that can be accessed by multiple threads concurrently. But as I know, System.Collections.Concurrent classes are designed for PLINQ. I have…
Arsen Mkrtchyan
  • 47,086
  • 29
  • 143
  • 178
23
votes
2 answers

Is the ConcurrentDictionary thread-safe to the point that I can use it for a static cache?

Basically, if I want to do the following: public class SomeClass { private static ConcurrentDictionary<..., ...> Cache { get; set; } } Does this let me avoid using locks all over the place?
michael
  • 13,854
  • 24
  • 84
  • 167
20
votes
3 answers

How to work threading with ConcurrentQueue

I am trying to figure out what the best way of working with a queue will be. I have a process that returns a DataTable. Each DataTable, in turn, is merged with the previous DataTable. There is one problem, too many records to hold until the final…
IAbstract
  • 18,848
  • 14
  • 81
  • 137
16
votes
4 answers

How to make updating BigDecimal within ConcurrentHashMap thread safe

I am making an application that takes a bunch of journal entries and calculate sum. Is below way of doing it is thread/concurrency safe when there are multiple threads calling the addToSum() method. I want to ensure that each call updates the total…
16
votes
3 answers

Is there an equivalent for Java WeakHashMap class in C#?

Is there a C# class that provides map with weak keys or/and weak values? Or at least WeakHashMap like functionality.
Daniel Sperry
  • 4,091
  • 3
  • 29
  • 40
15
votes
3 answers

Why are there no concurrent collections in C#?

I am trying to get an overview of the thread safety theory behind the collections in C#. Why are there no concurrent collections as there are in Java? (java docs). Some collections appear thread safe but it is not clear to me what the position is…
Andrew
  • 2,516
  • 13
  • 26
14
votes
4 answers

.NET 4.0 Concurrent collection performance

I am trying to write a program where i schedule items for removal by putting them in a collection from different threads and cleaning them up in a single thread that iterates of the collection and disposes the items. Before doing so, I wondered what…
Stijn Tallon
  • 380
  • 1
  • 3
  • 12
12
votes
3 answers

Concurrent collections eating too much cpu without Thread.Sleep

What would be the correct usage of either, BlockingCollection or ConcurrentQueue so you can freely dequeue items without burning out half or more of your CPU using a thread ? I was running some tests using 2 threads and unless I had a Thread.Sleep…
Prix
  • 18,774
  • 14
  • 65
  • 127
11
votes
1 answer

Usage of ConcurrentQueue>

I am basically looking for a container of image collections acquired from camera in a thread. Since ConcurrentQueue is thread-safe, I wanted to use it. But while debugging my code, I found this article saying If the elements are small, you’ll…
Tae-Sung Shin
  • 18,949
  • 31
  • 125
  • 230
9
votes
4 answers

How to sort a concurrent collection in .NET 4.0

How to sort a concurrent collection in .NET 4.0 For example I have constructed my ConcurrentBag collection. How can I sort the elements in it? ConcurrentBag stringCollection; ConcurrentBag customCollection;
mitul patel
  • 101
  • 1
  • 1
  • 3
1
2 3 4 5 6