Click here to Skip to main content
Click here to Skip to main content

A Perfect ThreadPool in Windows (C++)

By , 17 Apr 2013
 

Introduction

There are many thread pool implementations in the world. But a lot of them can not be fully controlled by the user (such as cancel a task at any time -- WaitForThreadpoolWorkCallbacks can not stop when task is running), and there are many limitations.  

So I wrote this FTL thread pool, this maybe the best thread pool implementation on Windows that you can find in the world. 

Background

Some thread pool implementations provided by Microsoft

  1. ATL::CThreadPool
  2. This is a simple IO completion port based thread pool, it will take long time when you stop the pool while there are some waiting jobs in the thread pool.

  3. QueueUserWorkItem

    This will create a global thread pool when call QueueUserWorkItem, and there is a manager thread in the pool. After more than 30 seconds that all the task have finished, the job thread will exit automatically.

  4. Vista thread pool

    This can run only on Vista/Win7, and the job threads *can not* exit automatically. 

And all the pool implementation *can not* cancel a single job at any time. I have written the demonstration program so you can verify it.

The characteristics of FTL thread pool are: 

  1. Can adjust current thread number automatically between min and max number according to the number of tasks; 
  2. Can cancel any task at any time, even if it's running;
  3. Can pause/resume/stop the whole thread pool; 
  4. Support feedback notification (such as error, progress) by callback interface;
  5. Implement by template, so you can translate params very easy 
  6. Support task priority when submit a new task (not support adjust it dynamic);
  7. Use the basic API, so can run under WinXP/Vista/WIn7, etc. (CreateThreadpoolWork can use after Vista)
  8. Support multi-instance (QueueUserWorkItem is single-instance) 
  9. There is no manager thread, so the cost is low.  

UML Diagram

Using the code

  1. Define the jomParam structure and write a subclass of FTL::CFJobBase
  2. class CMyJob : public FTL::CFJobBase<MyJobParam*>
    { 
        DISABLE_COPY_AND_ASSIGNMENT(CMyJob);
    public:
        CMyJob(MyJobParam* pMyJobParam);
        CMyJob();
        ~CMyJob();
        //overload function
        virtual BOOL OnInitialize();
        virtual BOOL Run();
        virtual VOID OnFinalize();
        virtual void OnCancelJob();
    };  
  3. Implement the IFThreadPoolCallBack callback interface if you want. 
     class CFtlThreadPoolDemoDlg : public CDialog,
      public IFThreadPoolCallBack<MyJobParam*>  
  4. Create the job instance and submit it into the threadpool, and can cancel it by pOutJobIndex at any time.  
    MyJobParam* pMyJobParam = new MyJobParam();
    pMyJobParam->m_nIntParam = ++m_nJobParamIntValue;
    pMyJobParam->m_nStrParam.Format(TEXT("SomeStringValue%d"), m_nJobParamIntValue);
    pMyJobParam->m_pThreadPoolDemoDlg = this;
    CMyJob* pNewJob = new CMyJob(pMyJobParam);
    m_pTestTheradPool->SubmitJob(pNewJob, &m_nCurJobIndex);  
  5. You can control the thread pool by following methods, It's very easy to understand by the function name.
  6. FTLINLINE BOOL Start(LONG nMinNumThreads, LONG nMaxNumThreads);
    FTLINLINE BOOL Stop();
    FTLINLINE BOOL Wait(DWORD dwTimeOut = FTL_MAX_THREAD_DEADLINE_CHECK);
    FTLINLINE BOOL SubmitJob(CFJobBase<T>* pJob, LONG* pOutJobIndex);
    FTLINLINE BOOL CancelJob(LONG nJobIndex);
    FTLINLINE BOOL Pause(); 
    FTLINLINE BOOL Resume();

Points of Interest

This is my first article in CodeProject, so if there are any issues, please tell me. 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

fishjam
China China
Member
No Biography provided

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
Hint: For improved responsiveness ensure Javascript is enabled and choose 'Normal' from the Layout dropdown and hit 'Update'.
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
GeneralMy vote of 5memberMihai MOGA10 May '13 - 18:21 
GeneralMy vote of 5membermagicpapacy1 May '13 - 17:52 
GeneralRe: My vote of 5 [modified]memberfishjam6 May '13 - 22:28 
GeneralMy vote of 5memberAnandChavali19 Apr '13 - 20:27 
GeneralRe: My vote of 5memberfishjam21 Apr '13 - 3:29 
GeneralMy vote of 5 [modified]memberskyformat99@gmail.com17 Apr '13 - 18:42 
GeneralRe: My vote of 5memberfishjam17 Apr '13 - 21:41 
GeneralRe: My vote of 5memberskyformat99@gmail.com19 Apr '13 - 4:27 
GeneralRe: My vote of 5memberfishjam21 Apr '13 - 3:20 

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

Permalink | Advertise | Privacy | Mobile
Web02 | 2.6.130513.1 | Last Updated 17 Apr 2013
Article Copyright 2013 by fishjam
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid