Click here to Skip to main content
15,891,896 members
Articles / Desktop Programming / MFC

A Programming Model to Use a Thread Pool

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
30 Sep 2000 147.3K   4.8K   48  
A class to manage the thread pool
// ThreadPool.h: interface for the CThreadPool class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_THREADPOOL_H__E60DFA90_8F3E_11D4_8AF0_0000C03A07C8__INCLUDED_)
#define AFX_THREADPOOL_H__E60DFA90_8F3E_11D4_8AF0_0000C03A07C8__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

struct IJobDesc
{
};

struct IWorker
{
	virtual void ProcessJob(IJobDesc* pJob)=0;
};


struct ThreadInfo
{
	HANDLE m_hThread;
	bool	m_bBusyWorking;
	ThreadInfo() { m_hThread=0; m_bBusyWorking=false; }
	ThreadInfo(HANDLE handle, bool bBusy) { m_hThread=handle; m_bBusyWorking=bBusy; }
	ThreadInfo(const ThreadInfo& info) { m_hThread=info.m_hThread; m_bBusyWorking=info.m_bBusyWorking; }
};


class CThreadPool  
{
	friend static unsigned int CThreadPool::ManagerProc(void* p); 
	friend static unsigned int CThreadPool::WorkerProc(void* p);
protected:
	enum ThreadPoolStatus { BUSY, IDLE, NORMAL };
public:
	//interface to the outside
	void Start(unsigned short nStatic, unsigned short nmax);
	void Stop(bool bHash=false);
	void ProcessJob(IJobDesc* pJob, IWorker* pWorker) const;

	//constructor and destructor
	CThreadPool();
	virtual ~CThreadPool();

protected:
	//interfaces public:
	HANDLE GetMgrIoPort() const { return m_hMgrIoPort; }
	UINT GetMgrWaitTime() const { return 1000*6; }
	HANDLE GetWorkerIoPort() const { return m_hWorkerIoPort; }

private:
	static unsigned int ManagerProc(void* p);
	static unsigned int WorkerProc(void* p);
protected:
	//manager thread
	HANDLE m_hMgrThread;
	HANDLE m_hMgrIoPort;
protected:
	//configuration parameters
	mutable UINT m_nNumberOfStaticThreads;
	mutable UINT m_nNumberOfTotalThreads;

protected:
	//helper functions
	void AddThreads();
	void RemoveThreads();
	ThreadPoolStatus GetThreadPoolStatus();
	void ChangeStatus(DWORD threadId, bool status);
	void RemoveThread(DWORD threadId)
	{
		CSingleLock lock(&m_arrayCs);
		lock.Lock();
		m_threadMap.RemoveKey(threadId);
		lock.Unlock();
	}

protected:
	//all the work threads
	CMap<DWORD, DWORD&, ThreadInfo, ThreadInfo&> m_threadMap;
	CCriticalSection m_arrayCs;
	HANDLE m_hWorkerIoPort;
};

#endif // !defined(AFX_THREADPOOL_H__E60DFA90_8F3E_11D4_8AF0_0000C03A07C8__INCLUDED_)

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions