Reputation: 10431
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
Updated again
Upvotes: 19
Views: 31831
Reputation: 5082
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:
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.
Upvotes: 49
Reputation: 14179
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).
Upvotes: 2
Reputation: 5085
You might want to read Herb Sutter's column 'Effective Concurrency'. You'll find those articles here, with others.
Upvotes: 8
Reputation: 43602
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
Upvotes: 6
Reputation: 89152
You might want to take a look at the parallel extensions for .NET
Upvotes: 8
Reputation: 24017
For C#, start learning the LINQ-way of doing things, then make use of the Parallel LINQ library and its .AsParallel() extension.
Upvotes: 5