views:

44

answers:

4

Is there any formula, maybe involving RAM & number of CPUs, which can give me a rough idea of how many threads I can spawn before it starts to be inefficient and slows the PC?

I want to load test another machine, so want to send requests as quickly as pobbile. But there's no point of spawning a million threads if they will just get in each other's way.


Edit: The threads are making Remote Procedure Calls (SOAP), so will be blocking waiting for the call to return.

A: 

First of all starting a thread may be quite a slow operation itself. When you start a thread stack space must be allocated, entry points in DLLs may be called etc. If you have a lot more threads than available cores then the majority of your threads will not be running at any given moment. I.e. they use resources and contribute nothing.

It is hard to give an exact number of threads for optimal performance, cause it depends on what the threads are doing, but generally you shouldn't go way above the number of available cores. Keep in mind that you cannot have more running threads than the number of available cores.

Brian Rasmussen
+3  A: 

It depends on what the threads are doing. If they're doing calculations, then the number will be lower. If they're waiting on I/O, then you can have more.

However, if they're waiting on I/O then you may be able to achieve the same result using async I/O apis better than using multiple threads.

Sam
+1 Thnaks. They are making Remote Procedure Calls (SOAP)
LeonixSolutions
+2  A: 

If all threads are active and not blocking waiting for something then basically one thread per CPU (core really). Any more than that and you're relying on the operating system to context switch between the threads on a given CPU.

But it all depends on what the threads are doing. If they're sleeping most of the time or waiting on asynchronous IO operations, then you mostly just need to worry about the memory used for the stack which defaults to about 1MB per thread I believe.

Josh Einstein
+1 Thnaks. They are making Remote Procedure Calls (SOAP), so wil be blocked waiting for the call to return.
LeonixSolutions
In that case you can achieve much better performance by using asynchronous operations. The WCF client proxy generation has the option (on by default if I recall) to create asynchronous versions of the SOAP methods. When you use these, Windows will use "IO completion ports" instead of blocking a thread.
Josh Einstein
+1  A: 

The other answers are of course correct; "it depends". If the threads are busy doing CPU-intensive work, there's no point having more than the number of cores available. But assuming they are waiting on external results, it can vary widely.

I often find that this question is answered by the architecture and requirements of an application; you need as many threads as you need.

But if you potentially have an unlimited number of threads you might end up spawning, I think that probably sounds like a task for the ThreadPool myself; let it decide how many threads to actually have running.

Andrew Barber
+1 Thnaks. They are making Remote Procedure Calls (SOAP). Threadpool sounds good as I can forego the startup costs of each thread at runtime; initialize the pool, then letar start the threads.
LeonixSolutions