Parallel For Loops

One first form of data parallelism is exposed by the Parallel.For set of methods, which provide a parallel for loop. The idea of a parallel for loop is to run the iterations of the loop in parallel, followed by a wait operation for all the iterations to complete. This is similar to the Parallel.Invoke construct we looked at in the “Parallel Invocation” section, earlier in this chapter.

An example of one of the simplest overloads of Parallel.For is shown here. Notice how similar this loop looks to a regular C# for loop, thanks to the concise syntax of lambda expressions:


This typical example of the parallel for loop is used to ...

Get C# 4.0 Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.