none
ThreadPool Thread Count ??? RRS feed

  • Question

  • Hi MSFT Moderators,

    I have a unserstanding about ThreadPool what I read in msdn i.e. threadpool limit is 25 worker threads per CPU and not per application instance. But today just for my test I went and wrote below code to test it. I complied the code and ran multiple instances but I was surprised that all the instances shows that 49 threads are available.

    So my doubt is : Is 25 thread from a thread Pool  per application instance is allocated in CPU ?? or there are 25 threads in threadpool per CPU and multiple instances should share among this 25 threads ??
     
    Can you please correct my code if I am making some mistake or can you give me some proper code snippet to clear my doubt.

    using System;
    using System.Threading;
    public class Example {
        public static void Main() {
            // Queue the task.
            ThreadPool.QueueUserWorkItem(new WaitCallback(ThreadProc));
           
            Console.WriteLine("Main thread does some work, then sleeps.");
             int workerThreads;
            int completionPortThreads;
            ThreadPool.GetAvailableThreads(out workerThreads, out completionPortThreads);
            Console.WriteLine("Current Threads available" + workerThreads.ToString() + " | " + completionPortThreads.ToString());
                Console.WriteLine("Main thread waiting.");
                Console.Read();
        }

        // This thread procedure performs the task.
        static void ThreadProc(Object stateInfo) {
            // No state object was passed to QueueUserWorkItem, so
            // stateInfo is null.

            Console.WriteLine("Hello from the thread pool. waiting ..");
              Thread.Sleep(100000);
             Console.WriteLine("Thread pool exited");


        }
    }

    Thanks in Advance...
    Best Wishes !!
    Thursday, October 23, 2008 10:08 AM

Answers

  • No, that's not the way the threadpool works.  The approach here is simply that two CPU cores would be capable of running twice as many threads.
    Hans Passant.
    • Marked as answer by Fey __ Tuesday, October 28, 2008 9:21 AM
    Friday, October 24, 2008 2:06 AM
    Moderator
  • According to the documentation, there is one pool per process, so in your case, each application instance would have it's own pool.


    http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx


    There is one thread pool per process. The thread pool has a default size of 25 worker threads per available processor, and 1000 I/O completion threads. The number of threads in the thread pool can be changed by using the SetMaxThreads method. Each thread uses the default stack size and runs at the default priority.


    Please mark the post if it answers your question.
    • Marked as answer by Fey __ Tuesday, October 28, 2008 9:21 AM
    Friday, October 24, 2008 6:39 PM

All replies

  • The thread pool has 2 type of threads: worker threads & I/O threads.

    Worker threads are used for asynchronous compute-bound operation.
    I/O threads are used to notify when an asynchronous I/O-bound operation has completed.

    The default value for worker threads after .net 2  is 25 and  1000 for I/O threads per CPU in Machine.(We can change this thread limit)
    Starting with .Net 2.0 SP1 the default worker threads is increased to 250.(.NET 2.0 SP1 was released with .NET 3.5)

    You can find the number of CPUs in machine using System.Environment.ProcessorCount.(This shows the number of logical CPUs)
    This value does not match exactly with your physical number of CPUs.
     For example a machine with one hyper-threaded CPU has 2 logical CPUs.

    Shirvan Noormohammadifar
    • Edited by Shirvan Thursday, October 23, 2008 12:54 PM
    Thursday, October 23, 2008 12:35 PM
  • The number of threads is based on the number of CPU cores.  You really need to update your version of the .NET framework.
    Hans Passant.
    Thursday, October 23, 2008 1:07 PM
    Moderator
  • Yes I have a dual core processor machine. and I agree with both of you ..

    but just want a simple clearification that if two instances of application are running simultaneously then each application has 25 worker thread (single processor machine) in their dedicated thread pool ??

    or

    There is 25 worker thread in thread pool which is commonly share between two instances of application ??
    Best Wishes !!
    Friday, October 24, 2008 1:50 AM
  • No, that's not the way the threadpool works.  The approach here is simply that two CPU cores would be capable of running twice as many threads.
    Hans Passant.
    • Marked as answer by Fey __ Tuesday, October 28, 2008 9:21 AM
    Friday, October 24, 2008 2:06 AM
    Moderator
  • According to the documentation, there is one pool per process, so in your case, each application instance would have it's own pool.


    http://msdn.microsoft.com/en-us/library/system.threading.threadpool.aspx


    There is one thread pool per process. The thread pool has a default size of 25 worker threads per available processor, and 1000 I/O completion threads. The number of threads in the thread pool can be changed by using the SetMaxThreads method. Each thread uses the default stack size and runs at the default priority.


    Please mark the post if it answers your question.
    • Marked as answer by Fey __ Tuesday, October 28, 2008 9:21 AM
    Friday, October 24, 2008 6:39 PM
  • Thanks I got it !!
    Best Wishes !!
    Tuesday, October 28, 2008 9:22 AM