C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
With it we estimate a good setting for minimum threads. We look at some code that could be used to improve the performance of certain applications.
Intro. First we need to see why we need a minimum thread setting. The reason is that Microsoft has some safeguards in their thread code that interfere. It does this to avoid excessive allocated memory.
This method tells Windows to keep a specific number of idle threads to anticipate new requests. The thread pool does not immediately begin creating new idle threads. MSDN
The interval MSDN states that the ThreadPool uses in the .NET runtime on Windows is half a second. In the software world, half a second is a long time. And this could interfere in program performance.
Example. There are two methods that are most important here. The first is ThreadPool.GetMinThreads, which accepts two out parameters. It is useful to get these values for diagnostics or for changing the actual values.
C# program that uses SetMinThreads using System; using System.Threading; class Program { static void Main() { // Get the numbers of minimum threads int w; int c; ThreadPool.GetMinThreads(out w, out c); // Write the numbers of minimum threads Console.WriteLine("{0}, {1}", w, c); // Change the numbers of minimum threads ThreadPool.SetMinThreads(20, c); } }
The code above will display 2, 2 on a dual core machine, but can display 4 on a quad core computer. Remember that multi-core computing is new and changing rapidly, so these are subject to change.
Note: Some technical details for this article were contributed by Neil Justice.
Benchmark. It is sometimes advantageous for you to use the SetMinThreads method. MSDN states that if your app has "bursts of activity" in which many threads are used at once, SetMinThreads improves performance.
Here: This benchmark runs tests of bursts of activity with 50 work items each time. It changes the minimum thread setting from 2 to 40.
C# program that benchmarks SetMinThreads using System; using System.Threading; class Program { static void Main() { // Loop through number of min threads we use for (int c = 2; c <= 40; c++) { // Use AutoResetEvent for thread management AutoResetEvent[] arr = new AutoResetEvent[50]; for (int i = 0; i < arr.Length; ++i) { arr[i] = new AutoResetEvent(false); } // Set the number of minimum threads ThreadPool.SetMinThreads(c, 4); // Get current time long t1 = Environment.TickCount; // Enqueue 50 work items that run the code in this delegate function for (int i = 0; i < arr.Length; i++) { ThreadPool.QueueUserWorkItem(delegate(object o) { Thread.Sleep(100); arr[(int)o].Set(); // Signals completion }, i); } // Wait for all tasks to complete WaitHandle.WaitAll(arr); // Write benchmark results long t2 = Environment.TickCount; Console.WriteLine("{0},{1}", c, t2 - t1); } } }
Increasing the number of minimum threads improved performance dramatically when it reached 5 and continued improving until 13. Please see the graph above for the results of this experiment.
Note: This benchmark is highly dependent on your machine's configuration. If you are reading this in 2015, you will need new benchmarks.
Summary. Here we saw how changing the number of minimum threads in the ThreadPool can improve performance on a dual core machine. This is most useful for programs with bursts of activity involving many threads.