Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does it make sense to spawn more than one thread per processor?

From a logical point of view an application may need dozens or hundreds of threads, some of which will we sleeping most of the time, but a very few will be always running concurrently. The question is: Does it make any sense to spawn more concurrent threads than processors there are in a system, or it is a waste?

I've seen some server applications that implement a scheduler to logically manage tasks (often called jobs), but also spawning a lot of threads, so I don't see where the benefit is.

Thanks in advance.

like image 970
Trap Avatar asked Feb 02 '09 15:02

Trap


2 Answers

Sure. If your software makes frequent use of disk or network IO, you can often improve throughput by adding a few more threads. Those extra threads will be awake and doing stuff while the other threads are blocking on IO.

like image 192
benjismith Avatar answered Sep 21 '22 04:09

benjismith


Other have talked about situations in which it almost certainly does make sense (when you are doing any kind of slow IO).

It might not be a good idea if:

  • your threads are doing CPU bound work

and

  • the threads each want to use a lot (i.e. significant compared to the cache size) of memory that does not overlap

In this case there is the possibility of causing unnecessary cache misses.

like image 42
dmckee --- ex-moderator kitten Avatar answered Sep 21 '22 04:09

dmckee --- ex-moderator kitten



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!