tags:

views:

692

answers:

9

As multi-core processors become more and more popular, I think it would be wise for me to become familiar with how to write code to take advantage of them. I am a .NET developer, and am not sure where to begin (seriously, I have no clue where to start). Any suggestions?

A: 

This tutorial seems promising: link text

Srdjan Jovcic
Is multi-threading guaranteed to utilize all processor cores?
Josh Stodola
@Josh Stodola: It depends on how many threads you end up creating. You actually run the risk of over utilizing the cores and not getting the best performance.
casperOne
For example, if you have 4 cores, and you create only one thread (in addition to main one), then your application will run on two cores. You have to do necessary legwork to create enough work in parallel to make all cores busy.
Srdjan Jovcic
@Josh: Also, there are libraries which allow you to do parallel operations on sets, but for start, you have to get familiar with threading in general.
Srdjan Jovcic
A: 

This might make a good starting place.

Joel Mueller
+4  A: 

The best way to start is to grab the latest CTP of the parrallel extensions framework and start plowing through their sample code.

JaredPar
Maybe parallel extensions are a bit over the top. Shouldn't begginer start a bit with manual threading, to understand all issues and learn basics?
Srdjan Jovcic
I am inclined to think that learning through these is better (thus my answer to look for info on .net 4.0). Basically you spend less time on how you hook everything up, and more time on how parallel affects the logic. And if no source happen to be available you can peek with reflector :)
eglasius
+1  A: 

Look for info on the .net 4.0. It will be adding different constructs for it.

eglasius
+2  A: 

Read up on the asynchronous programming model that .NET uses. Study the BeginXXX and EnxXXX paradigm that allows delegates to execute in the threadpool.

The Stream classes all have BeingXXX, EndXXX methods (eg BeginRead, EndRead) that allow you to do asynchronous IO. If you're working on something that heavily IO based you'll want to use these methods to increase parallelism.

Sean
"Read up on..." I take it you have an understanding of these paradigms. Do you have any material to suggest that I begin with?THANKS!
Josh Stodola
Take a look at http://msdn.microsoft.com/en-us/magazine/cc163467.aspxAnything by Jeffrey Richter on the subject is a good read. Also, seek out articles by Joe Duffy, such as http://msdn.microsoft.com/en-us/magazine/cc163467.aspx He's also got a good book on multithreading out.
Sean
+2  A: 

I don't recommend books very often anymore, but Joe Duffy's Concurrent Programming on Windows is simply irreplaceable.

Jekke
A: 

Simply separating your project into various threads specialized to do a particular function will let you take advantage of multiple cores. The OS handles scheduling the different threads to the most available processor core.

Daniel
+8  A: 

Multi-core programming is essentially multi-threaded programming. As long as your application is multi-threaded, the operating system will determine which core to run which thread on.

So then, your real question should be, what kind of applications would benefit from being multi-threaded? Think of such an application, then instantiate multiple threads to solve equal portions of the problem.

Dmitry Brant
To get started, I think I will write something that spins off four threads (I have a quad-core machine). Each thread will execute a separate console application that does some intense calculation for a minute or two (to spike CPU usage).I should expect 100% CPU usage in this case, correct?
Josh Stodola
That should be quite accurate.
Dmitry Brant
Thank you very much for the help! I have a pretty good understanding of this now. It makes sense that only certain types of applications would consider 4 cores to be mildly necessary.
Josh Stodola
+1  A: 

Like JaredPar said the Parallel Extensions are pretty good. I played with them a bit and they are easy to use and add performance.

The constructs I liked a lot were Parallel.ForEach() and PLinq.

Parallel.ForEach is similar to the ForEach(delegate) function that you have in DotNet 2.0 collections. It takes an action delegate and runs it in parallel.

Plinq allows you to execute any normal Linq query in a parallel fashion by adding .AsParallel() to the end of the query.

Check out the links Jared posted for more info.

Sruly