0

When I am learning Thread Pool in C# yesterday, I tried to create an example that will create a Thread Pool that enables two thread to enter the Thread Pool(MaxThreads) while give it 4 assignment in total. Here is my code:

    static void Main(string[] args)
    {
        ThreadPoolExample_0();
        while (true)
        {
            Thread.Sleep(2000);
        }
    }
    public static void ThreadPoolExample_0()
    {

        ThreadPool.SetMaxThreads(2,2);

        for (int i = 0; i < 4; i++)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(Task), i);
        }

        Console.ReadLine();
    }

    static void Task(object state)
    {
        Console.WriteLine("Task {0} is running on a thread from the thread pool.", state);
        Thread.Sleep(1000);
        Console.WriteLine("Task {0} exited.", state);
    }

However, it will output this dialog, which means that thread will enter the Thread Pool even it is full. Here is the dialog:

Task 3 is running on a thread from the thread pool.
Task 0 is running on a thread from the thread pool.
Task 1 is running on a thread from the thread pool.
Task 2 is running on a thread from the thread pool.
Task 0 exited.
Task 3 exited.
Task 1 exited.
Task 2 exited.

I wonder what's wrong with my code, or if my understanding of the C# thread pool is incorrect.

Han Han
  • 328
  • 12
  • 4
    [You cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the number of processors on the computer.](https://learn.microsoft.com/en-us/dotnet/api/system.threading.threadpool.setmaxthreads?view=net-7.0) Does your CPU have 4 cores? – shingo Jul 12 '23 at 04:57
  • @shingo thanks for answering my question! my CPU is `12th Gen Intel(R) Core(TM) i9-12900H 2.90 GHz` I tried to set max threads to 50 and give 200 tasks and that works. I wonder if I should delete this question or waiting for you(or someone else) to write a whole answer, so that other people who are facing the same issue can find a quick solution – Han Han Jul 12 '23 at 05:11
  • Related: [ThreadPool frustrations - Thread creation exceeding SetMaxThreads](https://stackoverflow.com/questions/11488966/threadpool-frustrations-thread-creation-exceeding-setmaxthreads). – Theodor Zoulias Jul 12 '23 at 05:19

2 Answers2

3

You cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the number of processors on the computer. If you try to do it, the ThreadPool.SetMaxThreads returns false, and the initial settings are preserved. It's a good idea to call the ThreadPool.GetMaxThreads after calling the SetMaxThreads, to verify visually that the ThreadPool has been configured according to your wishes. Just checking the return value of the ThreadPool.SetMaxThreads is not 100% reliable.

Theodor Zoulias
  • 34,835
  • 7
  • 69
  • 104
2

From the documentation on ThreadPool:

You cannot set the maximum number of worker threads or I/O completion threads to a number smaller than the number of processors on the computer. To determine how many processors are present, retrieve the value of the Environment.ProcessorCount property.

You probably have more than 4 CPUs, so setting the number of threads to 2 is a noop.

Mark Mucha
  • 1,560
  • 2
  • 12
  • 18