Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Ia m making a program that uses snmp to discover devices on a subnet. My program finds all ip addresses on the subnet and sends an snmp get request to each ip. I am using multiple threads and I am wondering:

If I don't limit the number of threads and there are more threads going than the cpu can handle how does the cpu deal with that, Does it wait to start a thread until one closes or do they just not get started at all?
Posted

As long as you don't run out of thread space you can have as many threads as you like.
But it won't make your application run faster because the CPU will spend more time switching context between the different threads than actually executing.

Regardless of which language you're using there's probably a thread pool available in some library that can help you with creating the right number of threads for the current platform.

/Fredrik
 
Share this answer
 
Both of those are good replys but they don't answer the question. The question was:

If I don't limit the number of threads and there are more threads going than the cpu can handle how does the cpu deal with that, Does it wait to start a thread until one closes or do they just not get started at all?

in my application i tried it with 255 threads and it completes in about 2 seconds. with 100 threads about 5 seconds with 10 threads about 1 minute. So the idea that more threads doesn't speed up the program is bogus.

I'm not looking for advice on how to use threads i'm wondering how the cpu handles threads when it can't handle them.
 
Share this answer
 
Even if you're using a thread pool (which is better than creation of numerous threads), having unlimited number of threads is usually a common fallacy. There are cases when you want unlimited number of threads (a typical example: demonstration of threading capabilities), but such cases are relatively rare.

Best schema is using fixed number of threads; and usually this is quite possible. (I don't say "constant number of threads"; number of threads can be defined in configuration file; after all file are created, their number is not changed anymore: example: you have N devices which require N * 2 + 1 threads.)
If you say "does it wait to start a thread until one closes", it is already the indication that in fact you don't need those two thread. You task also looks like you don't really need unlimited number of threads.

My recent answer will give you a better idea:
I'm trying to figure if I need to "restart" my thread each time new data comes in[^].

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900