Click here to Skip to main content
16,022,352 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
I'm not clear on how the whole threading process works with regards to the optimal number of threads to create. I've heard/read that I should only create a number of threads that corresponds to the number of processors my machine is running. Example: 4 threads for a quad core processor, 2 threads for a dual core processor, etc...

So what happens if I'm running a quad core machine and I create 10 threads to handle some really intensive calculations and database operations?

The reason I ask this is because I'm actually running a quad core machine and doing intensive math calcs and database operations (inserts and updates) in realTime and the only way that my app runs efficiently is with 10 threads. If I run anything less performance degrades.

Can someone please clarify to me what's going on?

Thanks in advance,

-Donald
Posted
Updated 18-Apr-11 9:36am
v2
Comments
AspDotNetDev 18-Apr-11 18:17pm    
Are each of your cores hyperthreaded? If so, that could be why you are seeing an improvement around 10 threads (4 cores x 2 = 8 virtual cores). Could also be that some of your threads are spawning on the same core (in which case, it would be somewhat random and would not really be related to the number of threads, though more threads might be more likely to end up on more cores).

The number of viable runnning threads really has nothing to do with how many cores you have since .Net uses the available cores as it sees fit. It also depends pretty much on what the threads are doing, and whether they're in a waiting state or actively processing something.

There is a break-even point somewhere, but I'm not sure if there's any reliable way to predict it short of having the app monitor it's own overall performance (of course, that would be handled in its own thread - grin), and adjust the maximum number of concurrently running threads on based on soe hard-coded threshold.

If ten is a good number in your environment, then ten is a good number.

.Net 4 introduced parallel tasks, which may (or may not) be more efficient.
 
Share this answer
 
Comments
d.allen101 18-Apr-11 15:51pm    
thanks for the reply john. just to give you a little more info, I'm basically running a blocking queue thats threaded and I'm running 10 threads to perform calculations on data then insert and update my database with the calc data. So what the correct way to determine the number of threads need? I found 10 by trial and error with my app? Also, how do I find out the "breaking point" for too many threads?
Sergey Alexandrovich Kryukov 18-Apr-11 17:43pm    
I agree with you. My 5. I pointed out some cases when you can get benefit of thread affinity, pretty marginal in my opinion.
Please see my Solution.
--SA
In addition to what John said: You can control of the affinity of some threads to some cores: use System.Threading.Thread.BeginThreadAffinity, System.Threading.Thread.EndThreadAffinity. At the level of the process, you can use System.Diagnostics.Process.ProcessorAffinity.

Doing it a normal situation is not recommended and would be usually pointless. One reason for such trick I can see is serving some "bad" hardware requiring heavy regular polling; it will make the load of other cores/CPUs more predictable.

—SA
 
Share this answer
 
Comments
Espen Harlinn 18-Apr-11 18:22pm    
Good points, my 5
Sergey Alexandrovich Kryukov 18-Apr-11 21:52pm    
Thank you, Espen.
--SA
El_Codero 5-Mar-12 18:16pm    
Thanks for sharing this trick. Best Regards
Sergey Alexandrovich Kryukov 5-Mar-12 18:28pm    
Thank you, Björn, but this is not a trick, and it is usually not needed.
--SA
El_Codero 5-Mar-12 18:33pm    
But it could be a trick if non standard hardware is used? Do you maybe know approximately how many percent it could be increased (generally)?
In your case the usual approach is to create one calculation thread for each core, offload IO to separate threads - possibly using separate computers for staging (including storing) of incomming data, and similarly try to offload storing of results to separate computers - often using a message queing solution.

Staging computer 1 ---
                        \
Staging computer 2 ---   \
                           calculating computer enques results to ---> storage solution  
Staging computer 3 ---   /

Staging computer 4 --- /


Moving IO out of the way is often the only real solution when you're doing heavy calculations. Database IO is *much* more expensive then enquing results to a message quing solution, especially if you're using something that handles load balancing in an intelligent way.

Regards
Espen Harlinn
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Apr-11 21:55pm    
Very good advices, my 5. However, tasks also should be considered and the effect or it... depends, I agree with John.
--SA
Espen Harlinn 19-Apr-11 8:09am    
Take a look at OPs' previous postings, as he is trying to deal with a fair amount of incomming data the calculations probably needs all available CPU, setting thread affinity and doing as little as possible, apart from the computations, is usually the only real option when you're dealing with quants in "real-time". In this scenario database access is often way too expensive to execute on the calculating/match searching computer. Spreading the load between several computers for a single instrument is often not an option due to IO overhead.

JSOPs' reply doesn't really show anything about how to get the most CPU cycles for the calculation as his approach often will degrade the computation by as much as 50%, maybe more.
Sergey Alexandrovich Kryukov 19-Apr-11 13:22pm    
Well, John does't try to show that, it's just a general considerations, one valid point is the possibility to use Task to make threading... well, implicit and declarative...
Your notes are very good. How I/O is arranged in threading model is really a key.
--SA
d.allen101 21-Apr-11 1:05am    
hey harlinn, can you point me to 'specific' threading topics that I need to understand in order to accomplish and understand exactly what you and SAKryukov are talking about? The two of you are talking over my head but I NEED/WANT to understand where you guys are coming from!
Espen Harlinn 21-Apr-11 9:24am    
I'll try to get back to you on monday, it's easter time :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900