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

98 questions
135
votes
5 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,507
  • 3
  • 11
  • 11
73
votes
2 answers

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…
66
votes
4 answers

What is the correct usage of ConcurrentBag?

I've already read previous questions here about ConcurrentBag but did not find an actual sample of implementation in multi-threading. ConcurrentBag is a thread-safe bag implementation, optimized for scenarios where the same thread will be both…
55
votes
7 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
  • 33,387
  • 34
  • 113
  • 219
43
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
  • 971
  • 1
  • 9
  • 14
41
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
  • 70,210
  • 21
  • 112
  • 164
30
votes
4 answers

Why doesn't ConcurrentBag implement ICollection?

I have a method which takes an IList and adds stuff to it. I would like to pass it a ConcurrentBag in some cases, but it doesn't implement IList or ICollection, only the non-generic ICollection, which doesn't have an Add method. Now, I…
Doron Yaacoby
  • 9,412
  • 8
  • 48
  • 59
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
  • 49,896
  • 32
  • 148
  • 184
25
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
  • 14,844
  • 28
  • 89
  • 177
21
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
  • 19,551
  • 15
  • 98
  • 146
17
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,381
  • 4
  • 31
  • 41
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
4 answers

Concurrent collections performance, confusing benchmark results

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…
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,598
  • 16
  • 27
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
  • 19,417
  • 15
  • 73
  • 132
1
2 3 4 5 6 7