title: Thread pool extreme perfomance lag
body: <p>There have been quite a few discussions on the issue, but they can't seem to explain my particular problem. <strong>I'm having serious performence issues when threading useing ThreadPool instead of Thread class.</strong></p>
  
  <p>The details: </p>
  
  <p>I have built a tcp server, when the tcp server accepts a new client it spawns a new thread to handle that client. All Fairly simple, but it takes my server way too long to handle many concurrent clients. 30 seconds for about 35 simple clients that just send a 2048 byte buffer, recieve it and close.</p>
  
  <p>After many stopwathes I found that <code>ThreadPool.QueueUserWorkItem</code> takes up to 26 seconds. I use it to spawn new threads for handling new clients.
  After replacing <code>ThreadPool.QueueUserWorkItem</code> with <code>new Thread()</code> my performance improved to less then a second.</p>
  
  <p>I would love some explanation to why this is happening.</p>
  
  <p><strong>Clarification:</strong></p>
  
  <p>The Delay has nothing to do with the client code, from the moment ThreadPool.QueueUserWorkItem is called until clientMsgHandler.HandleIncomingMsgs is started 20 seconds can pass.</p>
  
  <p>The lag starts at very first threads, and actually improves slightly as the test continues. I'm less interested in solutions and more interested in an explanation to why it's happening. 
  The client does block, but for a very short while.</p>
  
  <p>The server code:</p>
  
  <pre><code>private void AddTcpClientMsgHandler(TcpClient tcpClient)
      {
          //lock so no addition of client and closure can occur concurrently
          Stopwatch watch = new Stopwatch();
          watch.Start();
          Monitor.Enter(this);
          int pWatchIdx =  watchIDX++;
          if (!isOpen)
              throw new ObjectDisposedException(ResourceAlreadyClosed);
  
          TcpClientMsgHandler clientMsgHandler = CreateClientHandler(tcpClient);                                         
          clientMsgHandlerManager.AddTcpClientMsgHandler(clientMsgHandler);
          //ThreadPool.QueueUserWorkItem(clientMsgHandler.HandleIncomingMsgs); takes 20 seconds to run
          Thread thread = new Thread(clientMsgHandler.HandleIncomingMsgs);
          thread.Start();
          watch.Stop();
          Monitor.Exit(this);
          Console.WriteLine(string.Format("Iteration {0} took {1} Client {2}", pWatchIdx.ToString(),watch.Elapsed, tcpClient.Client.RemoteEndPoint));
  
      }
  </code></pre>