As a next step forward Microsoft is trying to introduce Parallel Extensions to various features of the .Net framework .
To try this, you need to first download Extension Libraries from following Location:-
www.microsoft.com/downloads/details.aspx?FamilyId=348F73FD-593D-4B3C-B055-694C50D2B0F3
I have started using this library in my applications and therefore will try to give some insight about how to use and what purpose it serves.
Intention behind Parallel For Loop :-
Parallel Extensions were designed to provide performance improves with multiple core or multiple processor machines. What it does is that various atomic operations like (For Loop) will be able to take advantage of multicore processors and there will not be any need of system programming to do that.
As of now processes can switch over from one core to another (taken care by Operating System) but what about threads within that process or atomic operations like this For Loop.
As an application developer, I could do least about this without getting into System Programming.
To help us to deal with this situation, Microsoft has came up with these Parallel Extensions.
I will not go deep into how it actually functions but will concentrate on how as a developer I can utilize this library.
How I can use it in my Application:-
Conventionally we use a for loop as follows :-
int noOfHashtables =4;
Hashtable[] hashtable = new Hashtable[noOfHashtables];
for(int i=0;i< hashtable.Length; i++)
{
hashtable[i] = new Hashtable();
}
Now with the help of Parallel Extension the code can be rewritten as follows :-
int noOfHashtables = 4;
Hashtable[] hashtable = new Hashtable[noOfHashtables];
Parallel.For(0, hashtable.Length, delegate(int i)
{
hashtable[i] = new Hashtable();
});
Few things about above code :-
- Above code is only supported in .Net Framework 3.5 and above.
- You will need library reference of this new Parallel Extension DLL which is downloadable (address of which is given in starting of the tutorial).
- Parallel.For(0, hashtable.Length, delegate(int i)
In this part of the code 0 is the starting value of variable i, so you can replace that with any value that you wish to.
To generalize the above code :-
we can rewrite the syntax as follows:-
Parallel.For(<your starting value >,<End criteria for loop>, delegate(int < your variable Name>)
{
// Your own code
});
More on this in my next article.