O'Reilly logo

C# 5.0 Unleashed by Bart De Smet

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

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:

double[,] xs = GetMatrix(300, 400);double[,] ys = GetMatrix(400, 500);double[,] zs = new double[300, 500];var sw = Stopwatch.StartNew(); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required