While using ConcurrentQueue, trying to dequeue while looping through in parallel

Posted by James Black on Stack Overflow See other posts from Stack Overflow or by James Black
Published on 2010-06-08T13:43:12Z Indexed on 2010/06/08 15:02 UTC
Read the original article Hit count: 226

Filed under:
|
|
|
|

I am using the parallel data structures in my .NET 4 application and I have a ConcurrentQueue that gets added to while I am processing through it.

I want to do something like:

personqueue.AsParallel().WithDegreeOfParallelism(20).ForAll(i => ... );

as I make database calls to save the data, so I am limiting the number of concurrent threads.

But, I expect that the ForAll isn't going to dequeue, and I am concerned about just doing

ForAll(i => {
    personqueue.personqueue.TryDequeue(...);
    ...
});

as there is no guarantee that I am popping off the correct one.

So, how can I iterate through the collection and dequeue, in a parallel fashion.

Or, would it be better to use PLINQ to do this processing, in parallel?

© Stack Overflow or respective owner

Related posts about concurrency

Related posts about c#4.0