Click here to Skip to main content
15,887,214 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.1K   4.8K   48  
A class to manage the thread pool
// ThreadPool2.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "ThreadPool2.h"
#include "ThreadPool.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;

using namespace std;

class CJob : public IJobDesc
{
public:
	CJob(int number):m_number(number) {}
	int GetNumber() { return m_number; }
protected:
	int m_number;
};

//we write a very simple worker
class CWorker: public IWorker
{
	virtual void ProcessJob(IJobDesc* pJobDesc)
	{
		CJob* pJob=reinterpret_cast<CJob*>(pJobDesc);
		for(signed __int64 i=0; i<1E8; i++)
			if (i%( pJob->GetNumber() )==0) TRACE1("found one is %d.", i);
	}
};

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0;

	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		// TODO: code your application's behavior here.
		CString strHello;
		strHello.LoadString(IDS_HELLO);
		cout << (LPCTSTR)strHello << endl;
	}

	int a;
	cin >> a;
	cout << endl;
	//create a thread pool and start it
	CThreadPool pool;
	pool.Start(2, 20);
	CWorker worker;
	for(int j=0; j<8; j++)
	{
		CJob* pJob=new CJob(13797+13*j);
		pool.ProcessJob(pJob, &worker);
	}
	cin >> a;
	pool.Stop();

	return nRetCode;
}


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