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

Using the code
- Define the
jomParam structure and write a subclass of FTL::CFJobBase
class CMyJob : public FTL::CFJobBase<MyJobParam*>
{
DISABLE_COPY_AND_ASSIGNMENT(CMyJob);
public:
CMyJob(MyJobParam* pMyJobParam);
CMyJob();
~CMyJob();
virtual BOOL OnInitialize();
virtual BOOL Run();
virtual VOID OnFinalize();
virtual void OnCancelJob();
};
- Implement the
IFThreadPoolCallBack callback interface if you want. class CFtlThreadPoolDemoDlg : public CDialog,
public IFThreadPoolCallBack<MyJobParam*>
- 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);
- You can control the thread pool by following methods, It's very easy to understand by the function name.
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.