Ideal Thread Count

BlitzMax Forums/BlitzMax Beginners Area/Ideal Thread Count

lukehedman(Posted 2009) [#1]
Another question to help me rebuild my framework threaded.

What is the ideal number of threads for a computer. I'd imagine the number of CPU cores a computer has would affect that.

I'm figuring the number of child threads should be about double or triple the core count. So if one thread running on a core stops because the data it needs is blocked, the other one or two can hopefully continue to run. I mean, assuming the program is built to take advantage of threads.

Still, I'm very inexperienced with this stuff. Any advice or suggestions?


xlsior(Posted 2009) [#2]
It depends on what you're trying to accomplish: If you have a need for several very processor intensive tasks, then one per core would probably be optimal.
If you have multiple low-CPU tasks then it really doesn't matter -- however many tasks are needed to accomplish your goal without them being in each other's way.


Nate the Great(Posted 2009) [#3]
I always try to use 2 or 4 threads for dual and quad core cpus respectively. I put all of the low cpu tasks in the same thread and the high cpu tasks in their own thread.


Dreamora(Posted 2009) [#4]
Depending on the operations, you might better calculate with 2-3 threads per core unless they are fully eating up the cpu time with pure math.
Because all memory fetching will introduce waiting times which you otherwise can waste a lot of potential power if your threads do a lot of work with memory.


TaskMaster(Posted 2009) [#5]
There is no way to know this. It really depends on how CPU intensive your threads are. But, you do not get to choose which core/cpu the thread is run on. So, even if you spawned 4 threads on a duo core CPU, it is possible all 4 of your threads ended up on the same core.


Dreamora(Posted 2009) [#6]
No its not, XP+ will move them between the cpus as needed. Naturally unless you set the cpu affinity to one / several specific cores only

Same goes for both *nix OS


ImaginaryHuman(Posted 2009) [#7]
One thing to keep in mind is that even if you have multiple cores in your computer, you still only have one memory and it has to be shared amongst all of the cores. So even if each core can do stuff in parallel with each other, they still are going to clash with each other when you try to access memory.


lukehedman(Posted 2009) [#8]
Alright. Thanks everyone!

Ugh... Designing a program to take advantage of threading is complex. It seems to me the architecture is a lot like programing a UDP networking app. Data doesn't come in order so a robust task manager helps. At least there's no packet loss :-D

Well, it's fun to figure these things out. Thanks again!


ImaginaryHuman(Posted 2009) [#9]
Don't forget about those corrupt packets? ;-D


Panno(Posted 2009) [#10]
stupid question :

is it possible to set the thread on one core and the rest to the other(s) ?


ImaginaryHuman(Posted 2009) [#11]
I think it is if you call o/s specific API functions. The o/s does an okay job of distributing the workload amongst the cores, though.


Dreamora(Posted 2009) [#12]
Yes you can, at least on Windows through the API
As you can set the Processor Affinity for the process, the same is possible for threads too.