Click here to Skip to main content
12,694,121 members (32,752 online)
Click here to Skip to main content
Add your own
alternative version


42 bookmarked

A programming model to use a thread pool

, 30 Sep 2000
Rate this:
Please Sign up or sign in to vote.
A class to manage the thread pool
<!-- Link to source file download -->
  • Download source files - 3 Kb
  • <!-- Link to demo file download -->
  • Download demo project - 24 Kb
  • <!-- Add the rest of your HTML here -->


    On many occasions we need to utilize mutiple threads to boost the system performance. The logic for each thread is almost the same, but we need to manage these threads. If the system is busy, we create more threads, otherwise we kill some thread to avoid the extra overhead.

    I have done a couple of projects involving the multiple-thread management. At last I decided to write a class to wrap this machenism. This class can dynamically allocate threads and assign jobs to these worker threads. You derive your own classes and you do not need to know the underlying machanism to handle the mutiple threading and synchronization between these threads. However, you need to make your worker classes thread safe since your objects may be assigned to different threads each time.

    The another thing I want to demonstrate is the using the feature of IOCompletion Port. I found that it is amazing easy and useful, especially when used as a way to transfer data between threads.


    To use the thread pool class you need to derive your worker class from IWorker and your job class from IJobDesc. The processing logic must be embedded within the member function IWorker::ProcessJob(IJobDesc* pJob). After you are finished, you can declare a thread pool like this:

    CThreadPool pool;
    pool.Start(6, 10);
    //do some other jobs here

    The Start function has two parameters. The first argument is the minimum number of the worker threads this thread pool object should spawn. The second argument indicates the maximum number of worker thread within this thread pool. If the thread pool is very busy working on the assigned jobs it will automatically spawn more worker threads. On the other hand, when the thread pool is idle some threads will be removed from the pool. Fine-tune these two parameters to get the best performance.

    To assign jobs to the thread pool for processing, simply call the function

    pool.ProcessJob(pJob, pWorker);

    You must make sure that your derived worker class is thread-safe since a worker instance may be on multiple threads simultaneously. You have no control as to whether the process is on the same thread as the last time or not.


    If the processing takes a very long time, when you call Stop(), the processing may not finished immediately. The Stop() function will wait for a maximum of 2 minutes and then return. This function has an optional argument. If this argument is set to true, the function will terminate these worker threads anyway. If this argument is set to false, these worker threads will not get terminated harshly and still live. Under this situation, you have to take care that the worker object may not exist after calling Stop() and you will get an access violation error if you attempt to access them.

    The job object must be generated on the heap using new operator. After the process ends it will automatically deleted by the framework. <p>


    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here


    About the Author

    Sherwood Hu
    United States United States
    No Biography provided

    You may also be interested in...


    Comments and Discussions

    GeneralA question about CCriticalSection Pin
    SeanQA28-Oct-08 21:26
    memberSeanQA28-Oct-08 21:26 
    GeneralWhen the menber function "Stop" is used twice and more Pin
    SeanQA28-Oct-08 21:24
    memberSeanQA28-Oct-08 21:24 
    GeneralComment on design... Pin
    Nigel de Costa5-Sep-06 0:02
    memberNigel de Costa5-Sep-06 0:02 
    GeneralRe: Comment on design... Pin
    alexquisi28-Mar-07 0:01
    memberalexquisi28-Mar-07 0:01 
    Generalhave memory leaks! Pin
    pcbirdwang3-Aug-05 15:43
    memberpcbirdwang3-Aug-05 15:43 
    GeneralATL Server provides its own thread pool class Pin
    Alexander Gräf6-May-04 5:10
    memberAlexander Gräf6-May-04 5:10 
    GeneralRemoveThreads bug Pin
    LAT26-Jul-03 10:44
    memberLAT26-Jul-03 10:44 
    GeneralRe: RemoveThreads bug Pin
    LAT27-Jul-03 1:54
    memberLAT27-Jul-03 1:54 
    GeneralRe: RemoveThreads bug Pin
    WREY27-Jun-04 7:46
    memberWREY27-Jun-04 7:46 
    GeneralLinker could not find "pthreadVC.lib" Pin
    WREY30-Aug-02 0:12
    memberWREY30-Aug-02 0:12 
    GeneralBug fixed! Pin
    XSimon25-Aug-02 17:27
    memberXSimon25-Aug-02 17:27 
    GeneralOther considerations Pin
    Bill Wilson21-Nov-01 12:48
    memberBill Wilson21-Nov-01 12:48 
    GeneralRe: Other considerations Pin
    Bill Wilson6-Dec-01 6:46
    memberBill Wilson6-Dec-01 6:46 
    GeneralRe: Other considerations Pin
    Bill Wilson6-Dec-01 7:05
    memberBill Wilson6-Dec-01 7:05 
    GeneralThis is more like a hack than a clean solution Pin
    zoly29-Jan-01 9:19
    memberzoly29-Jan-01 9:19 
    This code will not work on win64.

    The solution is not clean, it is more like a hack.

    The missuse of the followng function:

    BOOL PostQueuedCompletionStatus(
    HANDLE CompletionPort, // handle to an I/O completion port
    DWORD dwNumberOfBytesTransferred, // bytes transferred
    ULONG_PTR dwCompletionKey, // completion key
    LPOVERLAPPED lpOverlapped // overlapped buffer

    ::PostQueuedCompletionStatus(m_hWorkerIoPort, \
    reinterpret_cast<dword>(pWorker), \

    What about the semantics ???

    I have found a more acceptable solution (that J Richter calls a trick, not a hack)

    I recomend you to read the article from MSJ : New Windows 2000 Pooling Functions Greatly Simplify Thread Management from J Richter, he explains some nem threadpooling api from win2k, and how it is implemented.

    Win2k implementation uses similar tricks, but he solved the previus part more accptable :

    The CompletionKey associated with this device will be the address of the overlapped completion routine


    Also, because the address of the completion routine is the completion key, to get additional context information into the OverlappedCompletionRoutine function you should use the traditional trick of placing the context information at the end of the OVERLAPPED structure.

    So this way you can avoid assuming a pointer is a DWORD, and your code will survive win64.

    The article explains the win2k thread pool and it's flexibility, and performance enhancements it can bring. This article is the best description of win2k thraedpools that i've found and it is definitely better than the one from MSDN help.


    Anyway it is interesting Smile | :) .
    GeneralIOCP on Win16 based OSes Pin
    Jim Murphy4-Oct-00 5:42
    sussJim Murphy4-Oct-00 5:42 
    GeneralRe: IOCP on Win16 based OSes Pin
    connex22-Apr-01 6:03
    memberconnex22-Apr-01 6:03 
    QuestionIs there any benefit using 'struct' instead of 'class' to define the interface? Pin
    Anonymous26-Sep-00 9:36
    sussAnonymous26-Sep-00 9:36 
    AnswerRe: Is there any benefit using 'struct' instead of 'class' to define the interface? Pin
    Sherwood26-Sep-00 11:51
    sussSherwood26-Sep-00 11:51 
    AnswerRe: Is there any benefit using 'struct' instead of 'class' to define the interface? Pin
    Marius Cabas1-Oct-00 21:26
    sussMarius Cabas1-Oct-00 21:26 

    General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

    Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

    | Advertise | Privacy | Terms of Use | Mobile
    Web01 | 2.8.170118.1 | Last Updated 1 Oct 2000
    Article Copyright 2000 by Sherwood Hu
    Everything else Copyright © CodeProject, 1999-2017
    Layout: fixed | fluid