Click here to Skip to main content
Click here to Skip to main content

.NET's ThreadPool Class - Behind The Scenes

, 18 Mar 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
When to use .NET's ThreadPool class, and when to use something different.


My next installment of the Application Automation Layer requires a thread pool to manage worker threads. On investigating .NET's ThreadPool class, I discovered it is not quite what I had in mind. To quote from Manisha Mehta's article "Multithreading Part 4: The ThreadPool, Timer classes and Asynchronous Programming Discussed" found here[^]:

As you start creating your own multi-threaded applications, you would realize that for a large part of your time, your threads are sitting idle waiting for something to happen...

This is true only for a subset of threads, for example when an I/O completion event occurs and the thread is released. In many cases I require the ability to create threads that perform some work in the background while still allowing the user to interact with the application. And in specific, I'd like the ability to explore what I'll call "competitive threads", that is, adjusting thread priorities based on different factors. Think of it as "quality of service" for threads.

The article also states:

But remember that at any particular point of time there is only one thread pool per process and there is only one working thread per thread pool object...[A thread pool] has a default limit of 25 threads per available processor...

These statements seem contradictory, implying that only one thread can be executing per process, but that there are 25 threads available, all of which is very confusing. After investigating the code behind the ThreadPool, I have found that this statement is "sort of" true but also lead to some other discoveries.

The rest of this article discusses my observations. For purposes of understanding some of the numbers discussed, keep in mind that I am running these tests on a 1.6Ghz P4 single processor system. Also note that for all of these tests I am using a timer event set up to time out after one second. The timer event sets a flag which the thread monitors. When the thread sees the flag is set, it terminates itself. The code for the timer event is:

static void OnTimerEvent(object src, ElapsedEventArgs e)

How High Can I Count?

How high can I count in 1 second? Roughly 10,277,633. Using a System.Timers.Timer object set to trigger after one second, the the code happily counts until the timer expires.

static decimal SingleThreadTest()
  decimal counter=0;
  while (!done)
  return counter;

Why Use Thread Pooling?

Here's the reason to use thread pooling. This time, I'm going to see how high I can count creating and destroying a thread for every increment of the counter. Here's the code:

static void CreateAndDestroyTest()
  while (!done)
    Thread counterThread=new Thread(new ThreadStart(Count1Thread));
    while (counterThread.IsAlive) {};

static void Count1Thread()
And the answer is: 11 Yes. ELEVEN. In one second, my machine was able to create and destroy eleven threads. Obviously, if I have an application that needs to process lots of asynchronous, non I/O completion type of events, thread creation and destruction is a very expensive way to go. Hence the need for a thread pool.

First, A Benchmark

Before testing the performance of a thread pool, a benchmark is useful. I created one by simply instantiated 10 counting threads. Each thread increments its own counter, and at the end of one second, exits. Here's the code:

// initialize counters
static void InitThreadPoolCounters()
  for (int i=0; i<10; i++)

// initialize threads
static void InitThreads()
  for (int i=0; i<10; i++)
    threads[i]=new Thread(new ThreadStart(Count2Thread));

// start the threads
static void StartThreads()
  for (int i=0; i<10; i++)

// the thread itself
static void Count2Thread()
  int n=Convert.ToInt32(Thread.CurrentThread.Name);
  while (!done)
  Interlocked.Increment(ref threadDone);
...and the code that actually puts it all together:
while (threadDone != 10) {};
The resulting count for each thread is:

T0 = 957393
T1 = 1003875
T2 = 934912
T3 = 1004638
T4 = 988772
T5 = 962442
T6 = 979893
T7 = 777888
T8 = 923105
T9 = 982427
Total = 9515345

Within a reasonable margin of error, the 10 separate threads total to the same value as determined earlier by the single application thread counter. This therefore is our benchmark for the performance of a thread pool.

Using The ThreadPool

Now, let's see what happens when I use .NET's ThreadPool object:
static void QueueThreadPoolThreads()
  for (int i=0; i<10; i++)
    ThreadPool.QueueUserWorkItem(new WaitCallback(Count3Thread), i);

static void Count3Thread(object state)
  int n=(int)state;
  while (!done)
  Interlocked.Increment(ref threadDone);
The test, which is suppoed to run for only 1 second, takes something like 30 seconds to complete! And when it does complete, the counts are ridiculously high, indicating that the timer event never fired. To understand this, we have to dive into the sscli\clr\src\vm\win32threadpool.cpp code. Let's look first at ThreadPool.QueueUserWorkItem().

This function puts the worker thread delegate into a queue and tests whether a new thread should be created or not. If a new thread should be created, it calls the CreateWorkerThread function and exits. Conversely, if the a thread is not to be created at this point, a different thread, "CreateThreadGate" is created, if it doesn't already exist. The purpose of the thread gate is to periodically check and see if the thread can be created at a later time.

The ShouldGrowWorkerThread function performs a test of 3 parameters to determine whether or not a new thread should be created.

Note the the very first thing this function tests is to see if the number of running threads is less than the number of available CPU's. Obviously, this function will return false when there are one or more running threads on a single CPU system. When this is the case, (as per the flowchart) the thread gate is utilized to create the thread at a later time. I'll get to that shortly.

The CreateWorkerThread function is pretty much just a stub to instantiate the actual worker thread:

The worker thread sits in a wait loop awaiting a WorkRequestNotification event, timing out after 40 seconds if the event isn't signalled. Assuming the event is signalled, execution continues by removing the delegate from the queue (which places the event in the unsignalled state), testing whether a valid delegate was actually obtained, and then invoking the delegate. When the delegate returns, the worker thread immediately checks to see if there are additional requests queued. If there are, it processes those requests, and if not, it returns to the wait state.

From this code alone, a paramount fact of a ThreadPool managed thread is revealed: Do Your Work Quickly. Any time spent by your worker thread will result in delays when processing other items in the queue.

Now let's inspect the gate thread by looking at the GateThreadStart flowchart:

The first thing of note is that this function goes to sleep for 1/2 a second. After this delay, it performs a test to determine whether there are any thread requests in the queue. If not, it goes back to sleep. If so, it calls into a function to determine whether a thread should be initiated. This function delays creation of a thread based on the time the last thread was created and the number of currently running threads. By inspecting this table:

it is interesting to note that can take 5.4 seconds for the 25th thread to be instantiated. Furthermore, because of the Sleep(500) call, these times end up being quantized to 500ms intervals when threads are created in rapid succession. For example, if two threads are created in succession, the second thread will take 1000ms as the requisite 550ms will not have transpired and the function returns to the sleep state.

This section of the ThreadPool emphasizes the need to get in and out of your worker thread as quickly as possible so as to avoid the bottleneck that will occur if there are several concurrent threads running.

Timers And Waitable Objects

Timers callback using the ThreadPool. Therefore, if you want fairly reliable timers, they and your worker threads need to be short. Similarly, any waitable object, such as I/O completion events, that are managed by the ThreadPool are also affected by the implementation of the other threads in your application.

What Are The Alternatives?

As I mentioned at the beginning of this article, not all threads are created for the same purpose. While the ThreadPool is useful for managing threads that are usually in a wait state and that take only a short amount of time to do their work, .NET's ThreadPool class is a very poor choice for managing threads in situations that do not meet this criteria.

Fortunately, Stephan Stoub at Microsoft has written a ManagedThreadPool class that is designed for this second type of thread requirement. Using it is identical to .NET's ThreadPool:

static void QueueManagedThreadPoolThreads()
    for (int i=0; i<10; i++)
                          new WaitCallback(Count3Thread), i);
And as the following test numbers illustrate:

T0 = 970806
T1 = 996123
T2 = 914349
T3 = 990998
T4 = 977450
T5 = 957585
T6 = 951259
T7 = 770934
T8 = 982279
T9 = 1135806
Total = 9647589

it performs very well. The advantage of Stoub's ManagedThreadPool class is that all threads are initially created and assigned when needed. There is no complex holdoffs of thread creation, making this thread pool suitable for threads of both types.


I hope that this article has shed some light onto the complexity of thread pools. The attached code includes Stephan Stoub's code, unmodified. I leave it to the reader to further inspect his code, which is quite excellent.

And special thanks to CPian leppie for finding Stoub's code for me!


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Marc Clifton

United States United States
Marc is the creator of two open source projects, MyXaml, a declarative (XML) instantiation engine and the Advanced Unit Testing framework, and Interacx, a commercial n-tier RAD application suite.  Visit his website,, where you will find many of his articles and his blog.
Marc lives in Philmont, NY.

Comments and Discussions

GeneralMy vote of 5 PinmemberGerard Castelló Viader19-Dec-12 0:32 
QuestionNice Article about .nets threadpool class , nice eyeopener PinmemberRishiVC31-Jan-12 23:07 
GeneralMy vote of 5 PinmemberLizardM411-Feb-11 4:34 
GeneralExcelent Article PinmemberN a v a n e e t h27-Sep-07 2:01 
QuestionReading multiple files through MultiThreading Pinmemberarunikhanna8119-Jun-06 1:27 
GeneralThreadPool.SetMinThreads will help you PinsussAnonymous11-Aug-05 0:51 
GeneralRe: ThreadPool.SetMinThreads will help you PinmemberRon Inbar @ Home14-Oct-06 7:48 
GeneralCreating thread for a drawing Pinsussvivek91325-Apr-05 13:03 
GeneralRe: Creating thread for a drawing PinprotectorMarc Clifton25-Apr-05 13:12 
GeneralMax limit PinsussQudrat221016-Aug-04 4:15 
GeneralReturn data PinsussSpiros7-Apr-04 10:57 
GeneralGood article. Pinmemberhigon1-Jan-04 6:51 
GeneralRe: Good article. PineditorMarc Clifton6-Jan-04 9:42 
GeneralThanks! Pinmembertrageley26-Nov-03 11:50 
Generalquestion PinmemberTim Kohler9-Oct-03 7:00 
GeneralRe: question PineditorMarc Clifton9-Oct-03 7:41 
GeneralRe: question PinmemberTim Kohler16-Oct-03 19:07 
GeneralThread limit Pinmember5593836311-May-03 11:56 
GeneralRe: Thread limit PinmemberMarc Clifton1-May-03 15:13 
GeneralRe: Thread limit Pinmemberwolga27-Sep-03 17:01 
GeneralRe: Thread limit PinmemberHastarin19-May-05 17:24 
GeneralNice :) Pinmemberleppie20-Mar-03 7:07 
GeneralRe: Nice :) PinmemberMarc Clifton20-Mar-03 7:26 
GeneralRe: Nice :) PinmemberJames T. Johnson20-Mar-03 10:24 
Generalcareful what you measure Pinmemberspongman19-Mar-03 23:10 
GeneralRe: careful what you measure PinmemberMarc Clifton20-Mar-03 1:29 
Careful what you criticize! Big Grin | :-D
1) using decimal as a counter. I get a 3000% speed increase if I use an int instead.
The point of the article is not to test the performance of a thread, but to demonstrate the necessity of a thread pool and the inadequacy of .NET's ThreadPool class for certain requirements. As long as the same mechanism is used for all tests, the type of mechanism is irrelevant, as I am making relative comparisons, not absolute ones.
Simply put, I chose the decimal type intentionally because it is slower and I wanted manageable numbers.
2) using the correct timer class. you should use System.Threading.Timer instead of System.Timers.Timer. I get a 15% speed increase here.
As stated above, I am not interested in absolute performance. Since the System.Timers.Timer class is used consistently throughout all the tests, it does not affect the relative comparison of each test.
3) this is the big one: using thread.IsAlive in a loop...
I'll look at this in more detail, but keep in mind that even your measurement (670) proves my point, which is that thread creation and destruction is expensive and therefore necessitates some sort of a thread pool.
Just be careful what you measure when doing benchmarks like this. If you're trying to measure the number of threads you can create...
Well, that really wasn't the point of the article. And I considered my benchmarks carefully, by being consistent throughout so that I would obtain values that could be used to compare against for each mechanism demonstrated.
Thanks for the feedback,

Help! I'm an AI running around in someone's f*cked up universe simulator.
Sensitivity and ethnic diversity means celebrating difference, not hiding from it. - Christian Graus
Every line of code is a liability - Taka Muraoka
Microsoft deliberately adds arbitrary layers of complexity to make it difficult to deliver Windows features on non-Windows platforms--Microsoft's "Halloween files"

GeneralRe: careful what you measure PinsussYuriy Georgadze16-Jul-03 10:32 
GeneralYou're thinking again... PinmemberJeff J19-Mar-03 16:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411022.1 | Last Updated 19 Mar 2003
Article Copyright 2003 by Marc Clifton
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid