O'Reilly logo

C# Multithreaded and Parallel Programming by Rodney Ringler

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

Data parallelism on collections using Parallel.ForEach

The form of data parallelism that I find most helpful is performing concurrent operations on collections of data. This allows us to take data collections such as lists, dataviews, dictionaries, and so on and perform a task on each item in the collection in parallel with a single line of code! This makes using data parallelism simpler; you have to do nothing more than normal data processing. This is one of the reasons why the TPL is such a wonderful enhancement to .NET.

Much like the Parallel.For method, the structure of Parallel.ForEach looks like the following:

Parallel.ForEach(dataCollection, item => DoWork(item));

There are two parameters in the most basic version of this method. There is ...

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