Basic multi-threading questions in C#
I'm new to multithreading and I'm working on parallelizing an area in my application. I've read a number of posts on this site but I'm still confused as to the best way to approach my problem:
[1] In .NET 3.5, is ThreadPool
the only way for a program to exploit the multi-cores in the machine ? i.e. Is it possible to spawn threads on different cores using new Thread()
?
[2] My case: I have List<Calculation>
that contains around 80 items, currently processed sequentially. So given that I'm using .NET 3.5 and based on what I've read, ThreadPool
is probably my best bet for multithreading due to the high number of threads, however:
- There is a lot of dependency between the Calculations. Currently, the list is ordered in a way that all required Calculations are performed in the beginning.
This is how the dependency of the work items looks like (details not important, just wanted to make a point on the complexity of the dependenc开发者_运维问答ies):
- Calculation times are vastly different, one Calculation object might just involve retrieval of a value , other Calculation objects would involve a lot of work in nested loops...etc
How do I prioritize the main Calculations
that have something like 10+ other work items depending on it ? What is the most efficient way of signaling to use ?
Thank you.
Edit: I should mention that List<Calculation>
remains fixed. However, computing the 80+ calculations is called x million times. Every time an iterator is updated, Calculate() is invoked on every Calculation
in the list.
[1]: Yes, it is possible to spawn threads on different cores using new Thread()
, although you might be better served using the threadpool. The differences are discussed here:
Thread vs ThreadPool
This would be SO MUCH EASIER in .Net 4.0 using Task Parallel Library.
When are you scheduled to upgrade? It might be sooner than you could write all the required coordinating code yourself.
If you do have to do this brute force, you can use Thread.BeginThreadAffinity to ensure enclosed code runs on a single CPU. This should help your calculation performance.
[1] In .NET 3.5, is ThreadPool the only way for a program to exploit the multi-cores in the machine ? i.e. Is it possible to spawn threads on different cores using new Thread() ?
It's up to the operating system to decide which core a thread should run on. The OS will per default distribute threads evenly over multiple cores (at least in windows).
ThreadPool
are using the same kind of threads as the Thread
class, as there really are only one kind (but different types of classes and algorithms wrapping them)
[2] My case: I have Listthat contains around 80 items, currently processed sequentially. So given that I'm using .NET 3.5 and based on what I've read, ThreadPool is probably my best bet for multithreading due to the high number of threads, however:
Using ThreadPool.QueueWorkItem
seems to be the easiest way for you to convert your application.
Keep in mind that running 100 different threads doesn't say that your application will be 10 times faster than running 10 threads. It's more likely that 10 threads will run faster sine a lot if background stuff happens when .net and the OS switches between threads.
- I would convert the
List
into aQueue
- When launching your calculations, add 10 (or any number you like) calls to ThreadPool.QueueWorkerItem(MyMethod);
- In MyMethod, create a loop that continue to dequeue items from your queue until there are no more jobs left.
As mentioned in the comments, PLINQ makes this very easy.
List<Calculation> foo = ...;
foo.AsParallel.Select(c => c.Calculate());
精彩评论