Win NT ThreadPool

Posted on 1999-11-08
Medium Priority
Last Modified: 2010-04-01
How much overhead is involved in creating a new thread in Windows NT?  If I have an application that will start up sessions, each of which will use a thread, is there any advantage in creating a ThreadPool object to pre-allocate and then dispense these threads?  Is it efficient enough just to create and destroy threads as needed?
Question by:mcglinchey
  • 2
LVL 22

Accepted Solution

nietod earned 80 total points
ID: 2191655
There is quite a lot of overhead in creating threads.  Numerious tasks need to be done.  It needs a message queue, an entry has to be made in numerious "databases" that record thread information.  A task state structure needs to be created and initialized.  In C++ the RTL needs to be initialized for each thread.   Much more Most of this stuff needs to be reversed when the thread terminates.  

So there is a lot of overhead, but compared to what?  How long will the threads run?  What will they be doing.  If the thread does a lot of work, the the creation/destruction time is not signficant.  If it doesn't do a lot of work it may be significant, but you also might be better of not adding threads.

Let me know if you have any questions, of if you want to provide more info.

Author Comment

ID: 2191943
In particular, I'm interested in knowing if the overhead is appreciable compared with the overhead of maintaining a thread pool; eg a thread pool will need to maintain a semaphore, and will keep more threads than necessary in idle state.

To be concrete: the session may last for 5 minutes (so a thread's life might average ~ 5 minutes). There may be say 30 threads in a pool, and that would be the maximum expected # of sessions.  Most of the time, though, 80% of those threads will be idle.

Do you have a feeling for if keeping the threads' resources around and idle is more costly than the cost of creating and destroying threads as they're needed?
LVL 22

Expert Comment

ID: 2191961
For thread lives of that sort of length, you will not see any measurable benefit to having a thread pool.  The thread's useful length is 3 or 4 order of magnitude more than the time needed to create the thread.  (remember the 80-20 rule.  Even if it was the 1--99 rule, a thread pool wouldn't help.)

Futhermore, with that many threads (30) you would almost certainly be better off not having the threads sitting around doing nothing.  There is some cost to having suspended threads (Idle thread threads are obviously costly, but suspended ones are too.) and the more threads, the more the cost.  Note also that NT seems to have no problem with so many threads in a process (although it will be costly) Windows 95 would almost certainly not do well with so many threads in a process (things tend to break down in the 7-10 thread vicinity.)  I don't know if that matters to you

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

593 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question