Friday, January 15, 2010

Wednesday, February 18, 2009 C# Parallelism: Executing Methods in Parallel in .NET 3.5

Rob Volk's Dev Blog: http://blog.robvolk.com/2009/02/c-parallelism-executing-methods-in.html

Wednesday, February 18, 2009
C# Parallelism: Executing Methods in Parallel in .NET 3.5

To fully take advantage of multi-core processors and to speed up response times, developers have to embrace parallel design patterns. Most of the code we write today is in serial; we execute one method after another in a linear fashion. There are some pretty advanced libraries available to us thanks to the .NET Framework (e.g. ThreadPool, Thread), but for many reasons they are under utilized.

Coming in .NET 4.0 with Visual Studio 2010, we will get a whole new library that will allow us to easily take advantage of parallelism in our apps -- without drastically changing the code structure. I think this is the biggest selling point here. The .NET wizards at Microsoft took a look at the structure of our existing code and figured out some intuitive and non-intrusive ways for us to leverage parallel execution. This article has a very thorough explanation of the new classes.

For now, we can use existing classes like ThreadPool, Thread, and BackgroundWorker to execute code in multiple threads, which in turn should be processed in parallel from the multiple cores available on the system.

I've written a simple helper method that will execute chunks of code in parallel using ThreadPool. You can use the method in pretty much the same way as the Parallel.Do() method coming in .NET 4.0. So once you're on 4.0, just swap out the method calls.

Example usage:

ParallelProcessor.ExecuteParallel(()=>
{
// Get some data
}, ()=>
{
// get some different data
}, ()=>
{
// get some data from a web service
});

// do something else after all of the delegate methods have completedHere is code for ExecuteParallel:

public class ParallelProcessor
{
public delegate void Method();

///


/// Executes a set of methods in parallel and returns the results
/// from each in an array when all threads have completed. The methods
/// must take no parameters and have no return value.
///

///
///
public static void ExecuteParallel(params Method[] methods)
{
// Initialize the reset events to keep track of completed threads
ManualResetEvent[] resetEvents = new ManualResetEvent[methods.Length];

// Launch each method in it's own thread
for (int i = 0; i < methods.Length; i++)
{
resetEvents[i] = new ManualResetEvent(false);
ThreadPool.QueueUserWorkItem(new WaitCallback((object index) =>
{
int methodIndex = (int)index;

// Execute the method
methods[methodIndex]();

// Tell the calling thread that we're done
resetEvents[methodIndex].Set();
}), i);
}

// Wait for all threads to execute
WaitHandle.WaitAll(resetEvents);
}
}

No comments:

Shared Cache - .Net Caching made easy

All information about Shared Cache is available here: http://www.sharedcache.com/. Its free and easy to use, we provide all sources at codeplex.

Facebook Badge