views:

1493

answers:

9

I have built an application in C# that I would like to be optimized for multiple cores. I have some threads, should I do more?

Updated for more detail

  • C# 2.0
  • Run on Windows Vista and Windows Server 2003

Updated again

  • This code is running as a service
  • I do not want to have the complete code... my goal here is to get your experience and how to start. Like I say, I have already use threads. What more can I do?
+5  A: 

For C#, start learning the LINQ-way of doing things, then make use of the Parallel LINQ library and its .AsParallel() extension.

C. Lawrence Wenham
+6  A: 

You might want to take a look at the parallel extensions for .NET

http://msdn.com/concurrency

Lou Franco
+5  A: 

To be able to utilize multiple cores more efficiently you should divide your work up in parts that can be executed in parallel and use threads to divide the work over the cores. You could use threads, background workers, thread pools, etc

Lars Truijens
Only with threading I will be able to go on other Core? Other answer require Framwework 3.5, so you think I can do it with FrameWork 2.0?
Mister Dev
Yes, multiple threads or multiple processes, which is a thread in it's own way. And yes .Net 2.0 can do threads. Also notice you don't need the Thread class to make use of multiple cores, but in the end they all use threads.
Lars Truijens
A: 

Why I have -1 ? I might not understand this website :S

Mister Dev
A: 

As far as your -1, your question is pretty vague (it's been tagged with "too-vague" to demonstrate that). You've asked a pretty broad question, and there's no specific answer of what can be done to convert your program - they usually have to re-engineered with multi-threading in mind in order to work that way.

There are a number of great links if you search Google for "C# threading tutorial", and .NET provides some great namespaces for making multi-threading as easy as possible. I'd suggest you read some of the tutorials and get the general concepts down, and then you can think about how best to arrange your program so it's ready to be multi-threaded.

rwmnau
I think that was more solution than just threading. If no than sorry.
Mister Dev
+4  A: 

You might want to read Herb Sutter's column 'Effective Concurrency'. You'll find those articles here, with others.

Xavier Nodet
I would give you +1 but the website say I can't now cause I have less than 15. Thx for the answer.
Mister Dev
Sorry, can't help: I'm out of votes for today... ;)
Xavier Nodet
+21  A: 

I'd generalize that writing a highly optimized multi-threaded process is a lot harder than just throwing some threads in the mix.

I recommend starting with the following steps:

  1. Split up your workloads into discrete parallel executable units
  2. Measure and characterize workload types - Network intensive, I/O intensive, CPU intensive etc - these become the basis for your worker pooling strategies. e.g. you can have pretty large pools of workers for network intensive applications, but it doesn't make sense having more workers than hardware-threads for CPU intensive tasks.
  3. Think about queuing/array or ThreadWorkerPool to manage pools of threads. Former more finegrain controlled than latter.
  4. Learn to prefer async I/O patterns over sync patterns if you can - frees more CPU time to perform other tasks.
  5. Work to eliminate or atleast reduce serialization around contended resources such as disk.
  6. Minimize I/O, acquire and hold minimum level of locks for minimum period possible. (Reader/Writer locks are your friend)
    5.Comb through that code to ensure that resources are locked in consistent sequence to minimize deadly embrace.
  7. Test like crazy - race conditions and bugs in multithreaded applications are hellish to troubleshoot - often you only see the forensic aftermath of the massacre.

Bear in mind that it is entirely possible that a multi-threaded version could perform worse than a single-threaded version of the same app. There is no excuse for good engineering measurement.

stephbu
I would give you +1 but the website say I can't now cause I have less than 15. Thx for the answer.
Mister Dev
Wow that's exaclty what I wanted!
Mister Dev
+1  A: 

Understanding the parallelism (or potential for parallelism) in the problem(s) you are trying to solve, your application and its algorithms is much more important than any details of thread synchronization, libraries, etc.

Start by reading Patterns for Parallel Programming (which focuses on 'finding concurrency' and higher-level design issues), and then move on to The Art of Multiprocessor Programming (practical details starting from a theoretical basis).

McKenzieG1
+3  A: 

I asked a similar question before and here's the answers I got.

Schnapple