.Net Parallel FX Library

September 18th, 2008 by jason Leave a reply »

Microsoft is currently developing a new set of additions to .Net called the Parallel FX Library.  The library is intended to provide a better/easier API for multi-threading in .Net.  Anyone familiar with .Net will tell you that it already provides a rich threading library.  It is fairly easy to create and use threads in .Net using a number of approaches.  When I first read about the Parallel FX Library months ago, I was completely unexcited.  Last week I finally decided to take a look at the technology, and now I am excited.

Microsoft new libraries for threading is a giant leap forward for parallel tasks.  It is not a replacement for traditional, long running threads.  Instead, it is intended to help the developer offload tasks to multiple threads for performance gains on mult-core systems.  You cannot walk into a computer store and buy a new computer that isn’t at least dual core, so it makes sense that Microsoft would be targetting these platforms.

So far I have only explored the two areas of the Parallel FX Library, the basic Tasks framework, and the psuedo-looping mechanisms.

The Task is the basic unit of multi-threadedness in PFX.  The task encapsulates a delegate that will run on a thread.  The class Task is contained within the new namespace System.Threading.Tasks.

int x = 0;

//creates task, schedules for execution
Task task = Task.Create(foo => { x += (int)foo; }, 2);

//wait until execution is complete
task.Wait();

Console.WriteLine(x); //2

This example is very simple but illustrates the core concept that is the Task class.  As soon as Task.Create() returned the anonymous delegate was scheduled, and probably executing on a background thread.  The call thread.Wait() simply made the main thread hold until the value returned.  For those with a threading background, the Wait() statement will be obvious.

So what did this buy us?  Well, not a whole heck of a lot.  But this is just a very simple example.  Microsoft has built a lot of cool functionality on top of Task that will allow you to spawn all kinds of threads without having to manage them, all based on Task.

Consider the following example, which would not make very good production code but hopefully makes good example code:

int x = 0;
List<Task> tasks = new List<Task>();

//creates task, schedules for execution
for (int i = 0; i < 100000; i++)
{
Task task = Task.Create(foo => { x += (int)foo; }, i);
tasks.Add(task);
}

foreach (Task task in tasks)
task.Wait();

Console.WriteLine(x);

This code should run faster than a straight loop doing the same thing because these computations will be distributed to multiple threads which hopefully will be executed on multiple processor cores.  This code works, but I am still having to keep track of the tasks.  It seems like this should be done for me, and after a little digging I found that Microsoft has a class called Parallel in System.Threading.  This class has a few, very convenient static functions, For, For<>, and ForEach<>.  As you might guess, these functions act like loops.

int x = 0;
Parallel.For(0, 100000, foo => { x += foo; });
Console.WriteLine(x);

As you might guess, Parallel.For() is utilizing the Task class under the covers to execute the included anonymous delegate (lambda) to accomplish the same result as I wrote above.  The function signature for For takes an Action<int>, so we don’t need to cast foo to int.  The call does not return until all of the execution is completed.

Pretty cool so far.  We are now able to do repetitive operations on multiple threads with minimal code and no thread management.  For<> simply adds a generic type declaration for Action<>, and ForEach<> allows enumeration across a set of objects for the work.

I plan on digging into PFX more over the next week or two.  Some thought that this library might be release with .Net 3.5 SP1.  I would imagine that there will be another .Net release like .Net 3.6 or .Net 4.0 that will include PFX plus other works in progress like ASP.Net MVC.

Advertisement

Leave a Reply

You must be logged in to post a comment.