Creating a Thread Pool


The thread pool for this example will size itself based on the following conditions when a request is received:

  • If the CPU utilization is less than 20 percent and there are no free threads, then add another thread before adding the request to the queue. This will keep the CPU busy processing as many requests as possible.

  • If the CPU utilization is equal to or greater than 80 percent, shut down a thread before adding the request to the queue. This will slow down the number of requests that you process ( causing more to be queued) and hopefully give the CPU more time to concentrate on finishing the requests it s already processing.

This is a pretty basic algorithm, but it presents a good opportunity to illustrate how you can add and remove threads from the thread pool as well as obtain information about a running ATL Server application.

To implement this dynamic thread pool, you re going to have to be able to do the following:

  • You need to determine the maximum number of threads that your dynamic thread pool can support at any given time.

  • You need to be able to determine how many threads are being used to process requests at any given time.

  • You need to be able to determine what the CPU utilization is at any given moment.

  • You need to be able to add or subtract threads from your thread pool.

Luckily, you have the CThreadPool class to derive from to save you the work necessary to set up a thread pool. This lets you concentrate on implementing the preceding items. The code that you ll look at as you move through each of these sections has comments and error checking removed. This keeps the code shorter and easier to read. Please see the end of this section for a complete listing of the example.

Let s take a look at how you ll declare your thread pool class:

 template <class Worker = CIsapiWorker,            class ThreadTraits = DefaultThreadTraits >  class CDynamicThreadPool :      public CThreadPool< Worker, ThreadTraits >  {  } 

As you can see, the template signature matches that of the CThreadPool class. This makes it very easy to drop in your new thread pool class where you previously used CThreadPool .

The CThreadPool class has an initialization method declared as follows :

 HRESULT Initialize(void     *pvWorkerParam=NULL,                     int      nNumThreads=0,                     DWORD    dwStackSize=0,                     HANDLE   hCompletion=INVALID_HANDLE_VALUE)                     throw() 

You ll override this method to insert some initialization code. For this example, you ll use this method to initialize the code that you need to dynamically size your thread pool.




ATL Server. High Performance C++ on. NET
Observing the User Experience: A Practitioners Guide to User Research
ISBN: B006Z372QQ
EAN: 2147483647
Year: 2002
Pages: 181

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net