Setup of multithreads


I'm still a little unsure how to set up my threads.


I need multithreads because the server receives large file uploads, which can cause new requests to be blocked while the file is being received and handled.

I see two options:

(1) Either a thread collection that sleeps and gets activated when needed.

(2) Or simply just create a new threads when needed.


However, Read that it takes time to create new threads and that the CPU gets to work a lot while doing it. The time it takes to create a new thread doesn't really feel like a problem though, it happens in a millisec.

If I consider the first option, doesn't that if something create alot of CPU usage, to have lots of threads "just waiting" even if they're in "sleep mode"?


Hope someone has time to give some pros and cons of both methods

Best regards
Volang
Last edited on
What you are describing is a "thread pool". It's a fairly common way to handle this sort of thing. If you look up "thread pool", you should be able to find some easy C++ code to incorporate.
You could also fork child processes to handle these uploads.
#1 needs to be configurable without a rebuild / recompile so you can scale the program if you move to better hardware or get more traffic or whatever, if you use that idea.
#2 ... making threads takes time, there is an OS level request delay. its small until you have too many all at once.

a mix of the 2 ideas works best in my experience, keep N around all the time and add/destroy extras if you get a spike in traffic. for your scenario, maybe you can detect a file upload > some size and spawn a new thread for that and keep a group of high performance threads for the lighter traffic / smaller requests? There are tons of ways to do this stuff, tailor it for your traffic and needs and it will perform well.
Last edited on
Thanks all.

Jonnin: Big thanks. I'll probably go with the mixed version
Topic archived. No new replies allowed.