concurrent-collections

Another ConcurrentModificationException question

孤者浪人 提交于 2019-12-06 15:12:28
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 void recycle(){ messages.clear(); effects.clear(); proj.clear(); } } I'm adding and removing objects

Is ConcurrentBag cause of memory leak? [duplicate]

痴心易碎 提交于 2019-12-06 00:53:05
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(); foreach(int i in bag) //I know, I doing it wrong. { list.Add(i); } } What I expect: bag will be created

ConcurrentBag - Add Multiple Items?

我怕爱的太早我们不能终老 提交于 2019-12-04 15:03:27
问题 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<T> objectList = new ConcurrentBag<T>(); timeChunks.ForEach(timeChunk => { List<T> newList = Foo.SomeMethod<T>(x => x.SomeReadTime > timeChunk.StartTime); objectList.Concat<T>(newList); }); This code used to be in a Parallel.ForEach(), but I changed it to the above so I could

Concurrent collections eating too much cpu without Thread.Sleep

淺唱寂寞╮ 提交于 2019-12-04 02:54:03
问题 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 of at least 50~100ms it would always hit at least 50% of my CPU. Here is a fictional example: private void _DequeueItem() { object o = null; while(socket.Connected) { while (!listOfQueueItems.IsEmpty) { if (listOfQueueItems.TryDequeue(out o)) { //

Producer/consumer pattern with a fixed-size FIFO queue

余生长醉 提交于 2019-12-03 15:48:42
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 the queue at that moment in time and process them. For what it's worth, processing of the queued items by

ConcurrentBag - Add Multiple Items?

强颜欢笑 提交于 2019-12-03 10:23:38
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<T> objectList = new ConcurrentBag<T>(); timeChunks.ForEach(timeChunk => { List<T> newList = Foo.SomeMethod<T>(x => x.SomeReadTime > timeChunk.StartTime); objectList.Concat<T>(newList); }); This code used to be in a Parallel.ForEach(), but I changed it to the above so I could troubleshoot it. The variable newList indeed has objects, but after the objectList.Concat<> line,

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

倖福魔咒の 提交于 2019-12-03 08:35:24
问题 Basically, if I want to do the following: public class SomeClass { private static ConcurrentDictionary<..., ...> Cache { get; set; } } Does this let me avoid using lock s all over the place? 回答1: Yes, it is thread safe and yes it avoids you using locks all over the place (whatever that means). Of course that will only provide you a thread safe access to the data stored in this dictionary, but if the data itself is not thread safe then you need to synchronize access to it of course. Imagine

Concurrent collection supporting removal of a specified item?

谁都会走 提交于 2019-12-03 04:45:28
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 stage, further worked on, and stuffed into other queues. This process continues through a few more stages.

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

社会主义新天地 提交于 2019-12-03 00:00:53
Basically, if I want to do the following: public class SomeClass { private static ConcurrentDictionary<..., ...> Cache { get; set; } } Does this let me avoid using lock s all over the place? Yes, it is thread safe and yes it avoids you using locks all over the place (whatever that means). Of course that will only provide you a thread safe access to the data stored in this dictionary, but if the data itself is not thread safe then you need to synchronize access to it of course. Imagine for example that you have stored in this cache a List<T> . Now thread1 fetches this list (in a thread safe

Concurrent collections eating too much cpu without Thread.Sleep

送分小仙女□ 提交于 2019-12-01 15:17:10
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 of at least 50~100ms it would always hit at least 50% of my CPU. Here is a fictional example: private void _DequeueItem() { object o = null; while(socket.Connected) { while (!listOfQueueItems.IsEmpty) { if (listOfQueueItems.TryDequeue(out o)) { // use the data } } } } With the above example I would have to set a thread.sleep so the cpu doesnt blow up