Introduction to Parallel Programming in the .NET Framework

Why Parallel Programming

Historically Moore’s law has allowed us to largely ignore writing code designed to execute in parallel because the processor speed has continued to dramatically increase over time allowing our applications to run faster based solely on the hardware. Hardware has become increasingly more powerful and more readily available. However, clock speeds have leveled out now around 2.5 GHz. In order to provide increased performance, engineers have started adding more cores to the processor and more processors to each system. With more cores available, it means to get the next level of performance from your applications you would have previously gotten by purchasing a faster processor you will likely need to resort to writing code designed to use the multiple cores and execute in parallel rather than sequentially. Parallel programming, also commonly referred to as concurrent programming, is about writing code designed to execute multiple steps at the same time rather than single steps sequentially.

Think of parallel code as being similar to threading and running multiple threads, but the difference being they are also executing across different processors simultaneously. Microsoft released some items in the .NET Framework as well as in Microsoft Visual Studio that allows you to do more parallel programming and split tasks between processors. As a cautionary note, just because it is available and can be faster doesn’t mean that everything should be done in parallel. There are no guarantees made about the order in which the operations will execute. Often times you can get different results depending upon the problem you are solving.

The Parallel class has a number of static methods for performing multiple operations in parallel. We’ll focus the remainder of this article on taking a look at some of the static methods.

Parallel.Invoke

The Parallel.Invoke is a static method takes a parameter array of System.Action objects indicating what actions to execute in parallel. Each parameter is launched as a separate parallel task. It will work with methods, anonymous delegates, and lamda expressions as the input. The following sample demonstrates the concept of reading the contents of a file and then calling several static methods in parallel.


static void Main(string[] args)
{
string[] inputFiles = { “c:\test1.txt”, “c:\test2.txt” };
foreach (string file in inputFiles)
{
string fileContent = System.IO.File.ReadAllText(file);
Parallel.Invoke(
() => CountCharacters(fileContent),
() => CountWords(fileContent),
() => CountParagraphs(fileContent),
() => ProcessFile(fileContent)
);
}
}

static void CountCharacters(string content)
{
// implementation here
}

static void CountWords(string content)
{
// implementation here
}

static void CountParagraphs(string content)
{
// implementation here
}

static void ProcessFile(string content)
{
// implementation here
}


More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read