Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Basic approach to processing a list in parallel?

I'd like to do something that I assume is fairly basic, but I haven't found a fairly basic example yet. Here's what I want to do:

Given a list of objects, I want to iterate over each element in the list and call a method on it that's fairly resource intensive. I want to split this out over as many cores as are available to the running application, so that if it's running on an 8-core device, it's processing up to 8 elements at a time. All of the calculations are completely independent from each other, and I expect that as one element is done being processed, the next one in the list will get kicked off.

I'm not sure how many items will be in the list at any given time, and I don't know how many cores will be available. Maybe one one core at times.

Thanks.

like image 456
user1228457 Avatar asked Sep 18 '25 13:09

user1228457


2 Answers

Parallel.ForEach(items, currentItem =>
{
     // TODO: process a current item
     Debug.WriteLine("[{0}] {1} {2}", 
                    Thread.Current.ManagedThreadId , 
                    DateTime.Now,
                    currentItem);
};

items variable should be an instance of a type which implements IEnumerable or IEnumerable<T>.

Also you can specify ParallelOptions and MaxDegreeOfParalellism to control a number of threads which can be used by a ThreadPool while distributing a work between threads.

var options = new ParallelOptions
      {
         MaxDegreeOfParallelism = 2
      };

Parallel.ForEach(items, options, currentItem =>
{
     // TODO
});

Also see MSDN: How to: Write a Simple Parallel.ForEach Loop

like image 177
sll Avatar answered Sep 21 '25 03:09

sll


Rather then specifying the count, use the Environment to get so it will be as parallel as possible as capabilities increase.

Parallel.ForEach(
    items,
    new ParallelOptions {MaxDegreeOfParallelism =
        System.Environment.ProcessorCount
    },
    item => {
        // Work with item
    }
);
like image 24
Mike Avatar answered Sep 21 '25 04:09

Mike