-1
class Program
    {
        static void DoIt(string name)
        {            
            Console.WriteLine($"{DateTime.Now}, Thread{name} started");
            Thread.Sleep(50000);
            Console.WriteLine($"{DateTime.Now}, Thread{name} done");
        }

        static void Main()
        {
            //ThreadPool.SetMinThreads(100, 100);

            for (int i = 0; i < 100; i++)
            {
                int value = i;
                ThreadPool.QueueUserWorkItem((s) =>
                {
                    string name = value.ToString();
                    DoIt(name);
                });
            }

            Console.ReadKey();
        }
    }

The result of program: result

2021/4/26 11:26:23, Thread2 started
2021/4/26 11:26:23, Thread0 started
2021/4/26 11:26:23, Thread1 started
2021/4/26 11:26:23, Thread3 started
2021/4/26 11:26:24, Thread4 started
2021/4/26 11:26:25, Thread5 started
2021/4/26 11:26:26, Thread6 started
2021/4/26 11:26:27, Thread7 started
2021/4/26 11:26:28, Thread8 started
2021/4/26 11:26:29, Thread9 started

Start 100 threads at the same time. The first 4 threads are very fast. The latter threads start one every second util some working threads are done.

  1. Why first 4 thread are fast? The cpu cores of my computer is 4. If program runs in a 8 or 32 cores computer, the first 8 or 32 threads are fast, others are slow.
  2. if SetMinThreads to 100, all 100 threads start fast

Why ther latter threads start so slowly if i do not SetMinThreads?

Guanwei Li
  • 11
  • 3

1 Answers1

2
  • The .NET Threadpool will provide threads till the minimum (ThreadPool.GetMinThreads) is reached without any delay.
  • After that the threadpool can either wait for tasks to complete(queue) or create new threads (till it reaches ThreadPool.GetMaxThreads)
  • The actual algorithm of when the ThreadPool will create new threads is not documented as it keeps evolving. But does depends on the hardware it runs on and a bunch of stats it calculates.
  • By setting SetMinThreads to 100 you are essentially telling the .NET Threadpool to provide 100 threads, no questions asked. Thats why the threads start up fast.
  • Setting the MinThreads to a high value has its own drawbacks ...(you should see an increase in memory usage as each thread needs to have its own memory for callstack etc and as you reach a higher number of threads the processor will spend more time just doing context switching instead of the actual processing)
  • The default value of minthreads depends on the number of cores and the .net framework version. That's why when you move to different hardware the behavior changes.
Shinva
  • 1,899
  • 18
  • 25
  • Was going to say basically the same thing. Was going to add a link for the [`ThreadPool` source](https://referencesource.microsoft.com/mscorlib/system/threading/threadpool.cs.html) to illustrate the fact that thread pools can be complex and open to interpretation regarding how to handle min/max thread count. – txtechhelp Apr 26 '21 at 04:49