Add threads to thread pool
Hi All,
I am thinking a feature which can dynamically add threads to thread pool.
We can call it thpool_add_thread(int num);
How do you think about this proposal?
Thanks! hdl
Hi there.
What would the benefits of that be? Namely at the moment one initializes the threadpool depending on the thread support from the hardware - which is not supposed to change. I can't think of a reason that one might need to change it at runtime. Please feel free to correct me.
@Pithikos For example, actually is impossible to increase the number of threads. In my case, I need to assign one thread for each client in order as they arrived to my server.
@0x0soir I see your point. I am unsure though if this is the best way to solve it.
Having a thread for each client is fine for small projects but on production it is very inefficient due to the overhead that comes with it:
- By default each thread takes 8MB in stack memory (
ulimit -s). So 1000 threads will need 8GB in RAM. - Context switching.
So depending on your situation, for a small size of clients it should be totally fine to initialize the threadpool from the beginning to a specific number. A more ideal solution is to not have a thread-per-client setup at all due to the overhead mentioned above. As an example Nginx doesn't use multi-threading inherently but still it is popular for its speed.
Having said all that, we can still get the best of the two worlds. It makes sense to initialize the threadpool to something supported by your hardware and is optimized to your application needs (well beyond 100 threads in most cases). Then you can have connection handlers as work for the threadpool. The connection handlers are never dying and using select or similar to switch between connections.