Click here to Skip to main content
15,889,403 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hai All,

I am new to .net 4. So far I was using .net 2.0. I would like to know about Parallel programming in .net4.0. For example I have sample console application which would print 10000 numbers one by one using .net 2.0. How can I change this same program by using parallel programming.

Please explain parallel programming in .net4 with an example so that it would be better to understand. Please help me through c# code
Posted

Printing? Hm. Formally you can, using Parallel.ForEach (http://msdn.microsoft.com/en-us/library/system.threading.tasks.parallel.foreach.aspx[^]), but this is a very bad example. A console where you print it serialized the calls to, say, the method System.Console.WriteLine, so you will see pretty much the same, only some change in order is possible. A console is a poorly "parallelizing" resource.

The resource which you can effectively make parallel is the set of CPU cores. For true parallel execution, you need some fragments of code which are logically independent and can be executed not using any single shared resource. A simple example is a call some function with extensive calculation using only the data passed to it in its parameters, so all calculation is done in the function's own stack. As each thread works with a separate thread, shared resources are minimal, ideally only the CPU cores in case there are more then one, the parallel code will be most effective.

Please read some introductory material. Start here:
http://msdn.microsoft.com/en-us/library/dd537608.aspx[^].

Some useful background: http://en.wikipedia.org/wiki/Parallel_computing[^].

—SA
 
Share this answer
 
v2
Comments
Sander Rossel 23-Nov-11 2:49am    
Nice links, however I think you should've mentioned PLINQ (I mean the actual term), Task Parallel Library and Reactice Extensions since they are the core to multithreading in .NET 4. The Parallel ForEach was only an example of the OP or only one of his many questions. Please see my answer for a more details on Parallelism in .NET 4 :)
Sergey Alexandrovich Kryukov 23-Nov-11 12:31pm    
Sure, I agree, up-voted your answer.
--SA
Actually there are many articles concerning this topic right here on CP!
This article provides an overview and great links to other resources (be sure to download them!): Introduction to Parallelism in .NET 4.0[^]
Another one: Introducing .NET 4.0 Parallel Programming[^]
Explaning some concepts: Parallel Computing Concepts via C# 4.0[^]
Parallel Extensions for the .NET Framework - Part I (Introduction & PLINQ)[^]

Then there is the Task Parallel Library series by Sacha Barber:
Task Parallel Library: 1 of n[^]
Task Parallel Library: 2 of n[^]
Task Parallel Library: 3 of n[^]
Task Parallel Library: 4 of n[^]
Task Parallel Library: 5 of n[^]
Task Parallel Library: 6 of n[^]

And his article on Rx (Reactive Extensions): Fun with Rx[^]

MSDN also has great resources dealing with parallelism in .NET 4.0.
Parallel LINQ (PLINQ)[^]
Task Parallel Library[^]
Reactive Extensions[^]

I think this should prove sufficient :)
Good luck!
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 23-Nov-11 12:31pm    
Good resources, a 5.
--SA
Sander Rossel 23-Nov-11 13:18pm    
Thanks :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900