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:
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 dependencies):
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] 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.
List
into a Queue