Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ordered parallel execution

I have an ordered list like [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]. I am passing it to a Parallel.ForEach statement. Can I somehow achieve the following ordering of execution of buckets like: process first 3 items [1, 2, 3] where ordering in bucket itself is not mandatory and can be [2, 1, 3] for instance. Then process next 3 items [4, 5, 6], etc?

like image 898
Giorgi Nakeuri Avatar asked Mar 11 '23 01:03

Giorgi Nakeuri


1 Answers

I'm not sure that you can do this directly. but I would suggest you to divide the input list in to smaller lists and then you can process each sublist with Parallel.Foreach.

List<int> fruits = new List<int>() { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
List<List<int>> ls = new List<List<int>>();
for (int i = 0; i < fruits.Count; i += 3)
{
    ls.Add(fruits.GetRange(i, Math.Min(3, fruits.Count - i)));
}
foreach (List<int> group in ls)
{
    Parallel.ForEach(group, fruit =>
    {
    });
}

3 is the length of small list.

like image 95
vivek nuna Avatar answered Mar 21 '23 05:03

vivek nuna