C# 4.0 - Beginners look into parallel programming
Join the DZone community and get the full member experience.
Join For FreeUsed to we had to use multi-threading for something that resembled synchronously operations. This was the norm for programming, but things are changing and evolving and Microsoft has finally given us the ability to take advantage of multi-core processors on the systems we're programming for. Now to take advantage of this you will need the .Net 4.0 Framework and Visual Studio 2010. Before we go on I will let you know that there are extensions (Reactive Extensions for .Net) that allow you to access this from C# 3.5 but for the purpose of this article we're referring to 4.0 only.
NOTE: In all honesty you should only use parallel when you have a long, intensive process. If you use it for small, easy loops & tasks you could actuall slow down the overall process.
First think we want to do is create ourself a new Visual Studio 2010 project (I'm using a console application for this sample) so get that open and create your new project. Now we want to add a reference to the System.Threading.Tasks Namespace, to do this go Project > Add Reference and select System.Threading. Then in your code file add
using System.Threading.Tasks;This gives us access to what we need for parallel work. Noe, for this introduction we're going to count the number of files in a given directory, including the subdirectories in said directory. We're going to store all the directories in a Stack<string> just for simplicity sake. For those who have never used the Stack this is a LIFO (last-in-first-out) collection, in this case of type string.
For traversing the directories in a parallel fashion we'll use Parallel.Foreach Method, which executes a for each operation in a parallel iteration. We will use the overloaded version
Parallel.ForEach(TSource, TLocal) Method (Partitioner(TSource), ParallelOptions, Func(TLocal), Func(TSource, ParallelLoopState, TLocal, TLocal), Action(TLocal))During the parallel loop we will also use Interlock.Exchange that way we can keep the proper counter variable across multiple threads (atomic method). So here is how the method looks
public static List<string> GetAllFilesWithParallel(string root, Action<string> action)
{
//generic list for holding all the files
List<string> list = new List<string>();
//Count of files traversed
int i = 0;
//stack to hold all the file names
Stack<string> dirs = new Stack<string>();
try
{
//first make sure the directory provided actually exists
if (!System.IO.Directory.Exists(root))
{
Console.WriteLine(string.Format("The specified directory {0} could not be found", root));
return null;
}
//insert the root directory onto our stack (for later use)
dirs.Push(root);
//now we start looping
while (dirs.Count > 0)
{
//variables needed for this task
string currentDirectory = dirs.Pop();
string[] subDirectories = null;
string[] files = null;
//first get the subdirectories in the specified directory
subDirectories = System.IO.Directory.GetDirectories(currentDirectory);
//now get all the files in the current directory
files = System.IO.Directory.GetFiles(currentDirectory);
//add all the subdirectories onto the stack for traversal
foreach (string str in subDirectories)
dirs.Push(str);
//now use the Parallel.ForEach loop to get all the files in the current directory
//and add them to our generic list
Parallel.ForEach(files, () => 0, (file, loopState, localCount) =>
{
action(file);
return (int)++localCount;
},
(c) =>
{
Interlocked.Exchange(ref i, i + c);
});
}
}
catch (AggregateException e)
{
e.Handle((ex) =>
{
if (ex is UnauthorizedAccessException)
{
// Here we just output a message and go on.
Console.WriteLine(ex.Message);
return true;
}
return false;
});
}
return list;
}
To test our method we will do this in our main method. Here we will pass it the directory we wish to search and our Action. In this case our action will be adding the file names to our Generic list. We will prompt for the directory and get the files and count them. which looks like this
static void Main(string[] args)
{
List<string> files = new List<string>();
Console.WriteLine("Please provide the directory you would like to count:");
string root = Console.ReadLine();
Console.WriteLine("Counting files .....");
GetAllFilesWithParallel(root, (f) => { files.Add(f); });
if(files.Count != 0)
Console.WriteLine(string.Format("Total files in {0}", files.Count()));
// Keep the console window open.
Console.ReadKey();
}
Notice we pass the method an Action, a delegate than encapsulates a method with no parameters. We go this route so we dont have to explicitly declare a delegate. So from this beginners look you can see that parallel programming is a powerful tool offered from Microsoft, and can finally allow us as programmers to keep up (somewhat anyways) with the ever changing hardware world and performance they're offering.
So this is a beginners look at the new parallel programming ability in C# 4.0. Next we will take a more intermediate look at parallel programming and how it can increase performance in your applications.
Opinions expressed by DZone contributors are their own.
Comments