8

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?

8 Answers 8

13

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.

3
  • 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? Mar 3, 2009 at 20:38
  • That should be quite accurate. Mar 3, 2009 at 21:14
  • 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. Mar 4, 2009 at 4:32
3

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

2
  • 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? Mar 3, 2009 at 19:37
  • 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
    Mar 3, 2009 at 19:48
2

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.

2
2

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

1

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

1

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.

0

This might make a good starting place.

0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.