concurrent-collections

Why doesn't Collections.Generic.Queue have Synchronized method but Collections.Queue has?

纵饮孤独 提交于 2019-12-22 05:53:18
问题 System.Collections.Queue class has Queue.Synchronized method which returns a thread-safe Queue implementation. But the generic one, System.Collections.Generic.Queue does not have a Synchronized method. At this point I have two questions in mind: Why doesn't generic one have this method? Is it a framework API design decision? How is the queue returned from Queue.Synchronized is different than ConcurrentQueue<T> class? Thanks. 回答1: The Synchronized() method returns a wrapper queue that slaps a

Why are there no concurrent collections in C#?

痞子三分冷 提交于 2019-12-21 07:27:38
问题 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 for example with regard to: compound operations, safety of using iterators, write operations I do not want to reinvent the wheel! (I am not a multi-threading guru and am definitely not underestimating how hard this would be anyway). I hope the

Producer/consumer pattern with a fixed-size FIFO queue

陌路散爱 提交于 2019-12-21 04:53:45
问题 I need to implement the producer/consumer pattern around a fixed-size FIFO queue. I think a wrapper class around a ConcurrentQueue might work for this but I'm not completely sure (and I've never worked with a ConcurrentQueue before). The twist in this is that the queue needs to only hold a fixed number of items (strings, in my case). My application will have one producer task/thread and one consumer task/thread. When my consumer task runs, it needs to dequeue all of the items that exist in

Concurrent collection supporting removal of a specified item?

拟墨画扇 提交于 2019-12-20 17:29:50
问题 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 equality of an item or a predicate defining a condition for removal? Explanation: I have a multi-threaded, multi-stage workflow algorithm, which pulls objects from the DB and sticks them in a "starting" queue. From there they are grabbed by the next

Usage of ConcurrentQueue<StrongBox<T>>

拟墨画扇 提交于 2019-12-20 12:32:26
问题 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 probably never notice this. If, however, the elements hold on to large resources (e.g. each element is a huge image bitmap), it’s possible you could see the impact of this (one workaround is to queue a wrapper object, e.g. have a ConcurrentQueue

Usage of ConcurrentQueue<StrongBox<T>>

浪子不回头ぞ 提交于 2019-12-20 12:30:02
问题 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 probably never notice this. If, however, the elements hold on to large resources (e.g. each element is a huge image bitmap), it’s possible you could see the impact of this (one workaround is to queue a wrapper object, e.g. have a ConcurrentQueue

How to make updating BigDecimal within ConcurrentHashMap thread safe

放肆的年华 提交于 2019-12-17 22:40:52
问题 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 properly. If it is not safe, please explain what do I have to do to ensure thread safety. Do I need to synchronize the get/put or is there a better way? private ConcurrentHashMap<String, BigDecimal> sumByAccount; public void addToSum(String account

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

六月ゝ 毕业季﹏ 提交于 2019-12-17 17:43:08
问题 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 simple List. From what I understand, the ConcurrentBag works best when the reader and writer is the same thread. However, I hadn't thought it's performance would be so much worse than traditional locks. I have run a test with two Parallel for loops

Another ConcurrentModificationException question

房东的猫 提交于 2019-12-08 06:57:03
问题 I've searched StackOverflow and there are many ConcurrentModificationException questions. After reading them, I'm still confused. I'm getting a lot of these exceptions. I'm using a "Registry" setup to keep track of Objects: public class Registry { public static ArrayList<Messages> messages = new ArrayList<Messages>(); public static ArrayList<Effect> effects = new ArrayList<Effect>(); public static ArrayList<Projectile> proj = new ArrayList<Projectile>(); /** Clears all arrays */ public static

Is ConcurrentBag cause of memory leak? [duplicate]

假装没事ソ 提交于 2019-12-07 16:06:55
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Possible memoryleak in ConcurrentBag? I have epic memory leak in my app. All data, that i add in local concurrentBag collection in one of methods, was never collected. This simple code demonstrate, how I use it: void Main() { var l = new List<int>(){1,2,3,4}; Func(l); l.Clear(); l=null; } void Func(List<int> list) { var bag = new ConcurrentBag<int>(); Parallel.ForEach(list, k=> bag.Add(++k)); list.Clear();