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
1
vote
1 answer

Avoid or refer to old cache version when other reader thread is reading the collection in c#

I have cache implemented and this cache needs to be updated after certain intervals. Creating or updating cache involves calling lot many external APIs and may be little slower. My cache is a simple collection. I have made it static so that all the…
1
vote
0 answers

Blocking Collection GetConsumingEnumerable stops consuming increasing the size of the collection

I have a BlockingCollection to which a string is added every 25ms. There is a thread(ProducerConsumer()) for consuming the collection. This is running on a windows service. The GetConsumingEnumerable on the ProducerConsumer() thread stops consuming…
1
vote
1 answer

How to Insert in ConcurrentBag at specified Index and retaining all values?

I would like to get the same behavior as of List.Insert(Index, content ) In List , it just pushes the rest of elements forward while enables you to insert new element at specified index. But I am dealing Concurrency so I can't use List anymore…
Usman
  • 2,742
  • 4
  • 44
  • 82
1
vote
2 answers

AutoResetEvent process?

private ConcurrentQueue _queue = new ConcurrentQueue(); private AutoResetEvent _queueNotifier = new AutoResetEvent(false); public void MoreData(Data example) { _queue.Enqueue(example); _queueNotifier.Set(); } private void…
Prix
  • 19,417
  • 15
  • 73
  • 132
1
vote
1 answer

Fixed size ConcurrentQueue get element at index

I have found an old code which implement a thread safe queue. and I tried to create a new implementation using ConcurrentQueue Old code public class BoundedQueue where T : class { private readonly Queue _fixedSizeBuffer; private object…
Gilad
  • 6,437
  • 14
  • 61
  • 119
1
vote
1 answer

BlockingCollection - making consumer wait

Using the second example from Microsoft Docs, when I have a non-blocking consumer, what is the preferred approach to make consumer wait when there are no items in a BlockingCollection? The example from the docs is as follows. static void…
sakura-bloom
  • 4,524
  • 7
  • 46
  • 61
1
vote
1 answer

How to sort a ConcurrentBag?

I am working on a client/server application. The server sends messages to the client, but the order cannot be guaranteed. I am using TCP... I don't want to get into why the order cannot be guaranteed (it is to do with threads on the server). Anyway,…
pookie
  • 3,796
  • 6
  • 49
  • 105
1
vote
1 answer

ConcurrentBag TryTake method, which item is removed?

With reference from MSDN ConcurrentBag::TryTake method. Attempts to remove and return an object from the ConcurrentBag. I am wondering about on which basis it removes object from the Bag, as per my understanding dictionary add and remove…
bilal
  • 648
  • 8
  • 26
1
vote
1 answer

Why does BlockingCollection not implement ICollection?

The current implementation looks like this: public class BlockingCollection : IEnumerable, ICollection, IEnumerable, IDisposable Does anyone have an idea why it does not implement ICollection as well? It's kind of anoying...
AcidJunkie
  • 1,878
  • 18
  • 21
1
vote
1 answer

How to use tasks with ConcurrentDictionary

I have to write a program where I'm reading from a database the queues to process and all the queues are run in parallel and managed on the parent thread using a ConcurrentDictionary. I have a class that represents the queue, which has a…
ptn77
  • 567
  • 3
  • 8
  • 23
1
vote
3 answers

Concurrent collection happen-before relationship

I am now learning about concurrency, and I tried to write a programm which should demonstrate a happens-before relationship when using concurrent collection. As stated in java.concurrent package: The methods of all classes in java.util.concurrent…
1
vote
1 answer

Inheriting/Encapsulating a concurrent collection c#

I'm creating a custom collection that encapsulates a ConcurrentDictionary. I found a lot of information on encapsulating/inheriting from a generic collection but nothing specific to concurrent collections. Here is a code snippet of my base case,…
1
vote
1 answer

System.Collections.Concurrent missing classes

I've got the final release Visual Studio 2013 working on an MVC4 project. .NET 4.5 is the target platform. When I reference the System assembly all I see is BlockingCollection and ConcurentBag in the System.Collections.Concurrent namespace. …
Alex
  • 9,250
  • 11
  • 70
  • 81
1
vote
1 answer

ConcurrentKeyedCollection approach

Id like to make use of a ConcurrentKeyedCollection - but it doesn't exist. Why no ConcurrentKeyedCollection? What should I be using? (ConcurrentDictionary? Wrap my own concurrency protection around a KeyedCollection?)
Ricibob
  • 7,505
  • 5
  • 46
  • 65
1
vote
2 answers

Can many instances of an async task share a reference to a concurrent collection and add items concurrently to it in C#?

I'm just beginning to learn C# threading and concurrent collections, and am not sure of the proper terminology to pose my question, so I'll describe briefly what I'm trying to do. My grasp of the subject is rudimentary at best at this point. Is my…
Tim
  • 8,669
  • 31
  • 105
  • 183