Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Impact of hundreds of idle threads

I am considering the use of potentially hundreds of threads to implement tasks that manage devices over a network.

This is a C++ application running on a powerpc processor with a linux kernel.

After an initial phase when each task does synchronization to copy data from the device into the task, the task becomes idle, and only wakes up when it receives an alarm, or needs to change some data (configuration), which is rare after the start phase. Once all tasks reach the "idle" phase, I expect that only a few per second will need to wake.

So, my main concern is, if I have hundreds of threads will they have a negative impact on the system once they become idle?

Thanks. amso

edit:
I'm updating the question based on the answers that I got. Thanks guys. So it seems that having a ton of threads idling (IO blocked, waiting, sleeping, etc), per se , will not have an impact on the system in terms of responsiveness. Of course, they will spend extra money for each thread's stack and TLS data but that's okay as long as we throw more memory at the thing (making it more €€€)

But then, other issues have to be accounted for. Having 100s of threads waiting will likely increase memory usage on the kernel, due to the need of wait queues or other similar resources. There's also a latency issue, which looks non-deterministic. To check the responsiveness and memory usage of each solution one should measure it and compare.

Finally, the whole idea of hundreds of threads that will be mostly idling may be modeled like a thread pool. This reduces a bit of code linearity but dramatically increases the scalability of the solution and with propper care can be easily tunable to adjust the compromise between performance and resource usage.

I think that's all. Thanks everyone for their input.

--
amso

like image 209
amso Avatar asked Nov 03 '10 15:11

amso


People also ask

What happens if there are too many threads?

The Case of Creating Too Many Threads. Our job will take longer to finish if we generate thousands of threads since we'll have to spend time switching between their contexts. Use the thread pool to complete our task rather than creating new threads manually so that the OS can balance the ideal number of threads.

How do threads affect performance?

Each software thread requires virtual memory for its stack and private data structures. As with caches, time slicing causes threads to fight each other for real memory and thus hurts performance. In extreme cases, there can be so many threads that the program runs out of even virtual memory.

Do more threads always mean better performance?

Not necessarily. Achieving better performance in multi-threaded software is an exercise in bottlenecks. How many cores are available is one potential bottleneck. If there are more threads able to run than cores available to run them then more threads won't help performance and eventually become an overhead.

Do idle threads consume resources?

No. The idle thread doesn't consume CPU time (although starting the pool does take time, and it does consume memory).


2 Answers

Each thread has overhead - most importantly each one has its own stack and TLS. Performance is not that much of a problem since they will not get any time slices unless they actually do anything. You may still want to consider using thread pools.

like image 100
EboMike Avatar answered Sep 22 '22 08:09

EboMike


Chiefly they will use up address space and memory for stacks; once you get, say, 1000 threads, this gets quite significant as I've seen that 10M per thread is typical for stacks (on x86_64). It is changable, but only with care.

If you have a 32-bit processor, address space will be the main limitation once you hit 1000s of threads, you can easily exhaust the AS.

They use up some kernel memory, but probably not as much as userspace.


Edit: of course threads share address space with each other only if they are in the same process; I am assuming that they are.

like image 37
MarkR Avatar answered Sep 25 '22 08:09

MarkR