Is there a way to guarantee order when using Parallel.ForEach()
? The collection I am looping over needs to maintain it's order but I was looking for some performance improvement.
In order to retain the order you must try to order the list before passing it to foreach loop since by default the Parallel.Foreach treats the list as unordered.
Example :
Parallel.ForEach(
list.AsParallel().AsOrdered(),
(listItems) => {<operations that you need to do>});
So you have a statement that looks something like this? (based on your comments above).
Parallel.Foreach(myData, ..., (d) =>
{
StringBuilder sb = new StringBuilder();
sb.Append(d);
// WriteLine sb?
});
There are a number of issues with this approach.
Parallel.For
or Parallel.ForEach
will guarantee that your access to the contents of myData
are accessed in any particular order. StringBuilder
to either output the results or build up a complete string then you
are probably blocking on a shared resource, effectively serializing
parts of your parallel loop.It's hard to say more without seeing a concrete example of your code. Depending on what you are doing you might be able to use the order preservation AsOrdered()
in PLINQ to get where you want.
See this MSDN Resource and
Ordered PLINQ ForAll
This will allow you to return an ordered result set, based on the input order but not guarentee the ordering of the actual processing. However if parallel query is blocking on a call within the body you're unlikely to get good performance.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With