Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 dependencies):

alt text

  • 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.

like image 850
alhazen Avatar asked Jan 21 '26 08:01

alhazen


1 Answers

[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

like image 51
Simon Fischer Avatar answered Jan 22 '26 21:01

Simon Fischer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!