Infosys Microsoft Alliance and Solutions blog

« Session 0 and 1 in Vista | Main | WPF/E is now Silverlight »

Understanding .NET ThreadPool

We had some interesting discussions internally, post my earlier blog on 250 threads/CPU. There are some confusions also on default values and what comes from thread pool etc. I spent a little bit more time on basics and document them.

The ThreadPool contains threads marked as worker threads and IO threads (completion ports). The default max value for these are 25 threads / per process / per CPU. The default min values for these is #CPU, so on a single CPU machine it will be 1.

This means that each process has its own thread pool and not shared with other processes. However AppDomains loaded within a process share the threads in the pool. Do note that the default max and min are based on #CPU. On a dual proc box, the default max will be 50 and min will be 2.

The values can be altered by using ThreadPool.SetMinThreads and ThreadPool.SetMaxThreads. Obviously the min threads cannot be set to value higher than max threads. You can set this to a value lower than current #CPU, like you can set this to 0 on a single CPU box,  but doing that isnt' recommended and can cause performance impact, as also documented in the SDK.

Playing around with Min threads value is necessary sometimes to avoid delays in creating new threads for the thread pool. The delay is 500 ms per thread i.e. only 1 thread is added to the pool per 500 ms. Requests queued for threads will have to wait till a thread is created and is available for use. Idle threads in the pool die out with time.

The BackgroundWorker class introduced with .NET Framework 2.0 also uses threads from the thread pool class. The class has been added to easily work with background threads and on completion of the work, make updates to the UI controls. Note that UI controls can be accessed only on thread that they were created on.

ASP.NET also uses thread pool but has its own default values. The min remains the same, however with ASP.NET 2.0 the default max is 100 for worker and IO threads. These values are altered by appropriate setting the processModel tag in the machine.config file. Surprisingly the machine.config.comments file (available in <WINDIR>\Microsoft.NET\Framework\v2.0.50727\CONFIG) shows these as 20. This isn't entirely incorrect, since the machine.config file carries an auto configuration setting for process model, as below.


    <processModel autoConfig="true" />



If this auto configuration setting is removed, ASP.NET reverts to the max value of 20. With auto configuration enabled, on a dual CPU box, the max values will be 200.  New requests are handled by the IO threads. As the load increases, IO threads start to post the requests to internal queues and then worker threads take over and do the execution. Refer to this support webcast for more details on ASP.NET 1.0 and 1.0. For ASP.NET 2.0 you can check this blog post.

Comments are welcome !

Post a comment

(If you haven't left a comment here before, you may need to be approved by the site owner before your comment will appear. Until then, it won't appear on the entry. Thanks for waiting.)

Please key in the two words you see in the box to validate your identity as an authentic user and reduce spam.

Subscribe to this blog's feed

Follow us on

Blogger Profiles

Infosys on Twitter