Breaking Changes in the ThreadPool: The Movie

Well, my recent post on .NET 3.5 Brings BREAKING Changes to ThreadPool sparked quite a bit of interest in the .NET community.

But this is also something difficult to convince people of because it depends so heavily on configuration. The source code doesn’t change, the environment does.

So I’ve put together a screencast demonstrating the problem and elaborating further. If you doubt the validity of the previous post, or can’t reproduce the problem, please watch the video:

Download the video (approx 18 MB)

In the video I work with a modified version of the program. Here’s that for your enjoyment:

The application: NetThreading.exe (From Video).zip (2.59 KB)

The source code:

using System;
using System.Threading;

namespace NewThreadPoolBehavior
    internal class Program
        private static void Main(string[] args)
            Console.WriteLine( "Running on " + Environment.Version );
            int w, c;
            ThreadPool.GetMaxThreads( out w, out c );
            Console.WriteLine( "Max Currently: " + w + ", " + c );
            ThreadPool.GetMinThreads( out w, out c );
            Console.WriteLine( "Min Currently: " + w + ", " + c );

            Console.WriteLine( "Set min thread count 20? (y/n) " );
            string txt = Console.ReadLine();
            if ( txt == "y" )
                Console.WriteLine( "Setting min to 20" );
                ThreadPool.SetMinThreads( 20, 100 );
                ThreadPool.GetMinThreads( out w, out c );
                Console.WriteLine( "Min Currently: " + w + ", " + c );
            UseThreadPool( 200 );


        private static DateTime startTime;

        private static void UseThreadPool(int count)
            startTime = DateTime.Now;
            for ( int i = 0; i < count; i++ )
                    delegate { SlowMethod(); } );

        private static int concurrent = 0;

        private static void SlowMethod()
            TimeSpan dt = DateTime.Now - startTime;
            Console.WriteLine( "Starting ops (" + concurrent + " concurrent, elapsed=" +
 dt.TotalSeconds.ToString( "N3" ) + " sec.) " );
            Thread.Sleep( 20000 );
            Console.WriteLine( "Finished ops (" + concurrent + " concurrent)" );

.NET 3.5 Brings Breaking Changes to ThreadPool

[Note: This has been fixed in .NET 3.5 SP1, read more on this post.]

Holy smokes! I thought we had figured out something significant when I posted .NET 3.5 Brings Major (Undocumented) Changes to ThreadPool where we discovered that the .NET 3.5 ThreadPool changed the allocation algorithm for adding threads from linear to logarithmic.

This is bigger. [Recently updated see note below]

Here’s the scenario. I have a server – say in the financial sector – that must process many requests and it must get up to speed immediately. I can’t pay the 500 ms warm up time for the ThreadPool (.NET 2.0) or the even slower model in .NET 2.0 SP1. What do I do? I call ThreadPool.SetMinThreads(x, x) where x < (the current max), but much higher than 2 (the default). So I might call ThreadPool.SetMinThreads(100, 100) or something like that.

In .NET 1.0 – .NET 2.0 (without SP1) you would go from the warm up time model:

                 warm up time model (.NET 2.0)

To this immediate “ready” threading model.
                 ready model (.NET 2.0)

Continue reading

.NET 3.5 Brings Major (Undocumented) Changes to ThreadPool

It was all going so smoothly. Jason Whittington, Mark Smith and I were teaching the big DevelopMentor event here in Los Angeles (Guerrilla.NET) when my presentation on the ThreadPool took a nose dive. It started with a great joke involving Wilson (the volleyball from Cast Away).

Wilson and I built an application to compute a multiplication table where each computation was (artificially) slow. To speed it up we threw it at the thread pool using delegate.BeginInvoke. We figured that the ThreadPool would allocate 25 or so threads and the table would display quickly. Here’s the expected output - pretty much the same thing we’ve seen since about .NET 1.0:

Each color represents the thread that did that computation.

For the last 7 years, the behavior has been that as the ThreadPool was overloaded, it would steadily start up new threads at the rate of one every 500 milliseconds until it hits its upper limit (typically). Using Performance Monitor (perfmon) we can watch the thread pool adding threads. It usually looks something like this:

Continue reading