TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to C-SHARP

C# ThreadPool.SetMinThreads Method

Call the SetMinThreads method on ThreadPool to improve performance with multiple threads.
SetMinThreads alleviates some bottlenecks with ThreadPool. 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.Threads
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.

Quote: 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 (Microsoft Docs).

The interval Microsoft 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.

Info: ThreadPool.GetMinThreads accepts two out parameters. It is useful to get these values for diagnostics or for changing the actual values.

Result: The code will display 2, 2 on a dual core machine, but can display 4 on a quad core computer.

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); } }
Benchmark. It is sometimes advantageous for you to use the SetMinThreads method. Microsoft 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. I suggest testing the code yourself.

Sleep

Info: Some technical details for this article were contributed by Neil Justice.

Summary. 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.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf