Last night, I was playing around with some cool new features of .net framework 4.0, which a CTP released as a VPC, can be downloaded from here.
There are many new stuffs Microsoft planned to release with Visual Studio 10. Parallel Extension is one of them. Parallel extension is a set of APIs that is embedded under the System namespace and inside mscorlib assembly. Therefore, programmers do not need to use a reference from any other assembly to get benefit of this cool feature. Rather they will get this out of the box.
Well, what is Parallel Extensions all about?
Microsoft’s vision is, for multithreaded application, developers need to focus on many issues, regarding managing the threads, scalability and so on. Parallel extensions are an attempt to allow developers to focus onto the core business functionality, rather the thread management stuffs. It provides some cool way to manage concurrent applications. Parallel extensions mainly comprises into three major areas, the first one is the Task Parallel library. There is a class Task which the developer should worry about. They will not bother about the threads; rather they will consider that they are writing tasks. And the framework will execute those tasks in a parallel mode. The next major area is called PLIQ, which is basically a LINQ to Objects that operates in parallel mode. And the third one is Coordination data structure.
Let’s have some code snippet to get a brief idea about this.
We will use a simple console application, and see the solution explorer we are not using any assemblies as opposed to the defaults.
So parallel extensions do not require any special libraries!
The above code does, takes an input integer and doubles that, and finally finds the prime numbers from zero to that extent. Well, this is nothing quite useful, but enough to demonstrate an application. This method also writes the executed thread ID into the console window.
Now, let’s first create few threads to execute our above written method. We will create 10 threads to execute the methods simultaneously.
Here things to notice that, in this way, we have the thread instance under our control, so we can invoke methods like, Join(), Abort() etc. but, developer is responsible to manage threads by their own. The code produces following outputs.
See, we have actually 10 different threads generated to execute this. Now, let’s use the Thread Pool thread for the same business.
This generates the output like following.
Look, it is using the same thread (6) for all the work items. The .net thread pool using thread objects effectively. But in this way, we lost the control that we had into the previous snippet. Like, now we can’t cancel a certain thread directly, because we actually don’t know which thread is going to execute the work items.
Now, let’s have a view into the cool parallel extensions Task class. It’s pretty much like the Thread implementations, and allows all the methods like, Wait(), CancelAndWait() etc to interact with the task. In addition, it will take advantage of the execution environment. That means, if you run this application into a Multi core processor, it will spawn more threads to carry out the tasks. Though, as I am using this into the VPC with a single core CPU, it’s using one thread instance to carry out the tasks. But now this is not my headache, to manage threads, or even thinking about it. All these concerns are taken care of by the Parallel Framework. Cool!
This generates the same output like Thread Pool snippet, but that is only because I have used it into a VPC. On a multicore machine, it will generate more threads to get the optimal performance.
Parallel Static class
Well, this is even more interesting. It offers few iterative methods that automatically executes each iterations as a task and of course in parallel. Isn’t it a cool one?
I hope. I will explain PLINQ in my next post. Happy programming!