This is not necessarily as simple as using a thread pool.
By default, a thread pool allocates multiple threads for each CPU. Since each thread participating in the work that you do has a cost (overhead for tasks, using a very limited CPU L1, L2, and possibly L3 cache, etc.), the optimal number of threads used is = = number of available CPUs - unless each thread requests services from other machines - for example, a highly scalable web service. In some cases, especially when it comes to reading and writing to the hard drive than the processor, you may be better off with 1 thread than with multiple threads.
For most applications and, of course, for encoding WAV and MP3, you should limit the number of workflows to the number of available CPUs. Here is the C # code to find the number of processors:
int processors = 1; string processorsStr = System.Environment.GetEnvironmentVariable("NUMBER_OF_PROCESSORS"); if (processorsStr != null) processors = int.Parse(processorsStr);
Unfortunately, this is not as simple as limiting the number of processors. You should also consider the performance of the controller (s) of the hard disk and disks (disks).
The only way to find the optimal number of threads is a trial error. This is especially true if you use hard drives, web services, etc. With hard drives, you might be better off not using all four processors on your quad-core processor. On the other hand, with some web services you might be better off making 10 or even 100 requests for the processor.
Joe Erickson Feb 20 '09 at 1:54 2009-02-20 01:54
source share