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

A Method of Worker Thread Pooling

Rate me:
Please Sign up or sign in to vote.
3.63/5 (20 votes)
5 Feb 20036 min read 145.5K   4.2K   78  
A Method of Worker Thread Pooling
// ThNode.cpp: implementation of the CThNode class.
// Author : Pradeep Kumar Sahu
// Email  : sahupk@yahoo.com
// Note   : It will be greaet to receive your feedback and any bugs that 
//          are found.
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ThreadPool.h"
#include "Thread.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
#define STOP_WORKING -1
#define KEEP_WORKING  0

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
//UINT ThreadFunction(LPVOID );
CCriticalSection  CThread::m_threadCS;

CThread::CThread(CThPool *pPool)
{
	//Start the thread 
	this->m_pPool = pPool;
	this->m_pTarget = NULL;
	this->m_pThread = AfxBeginThread(ThreadFunction,this);
	this->m_flag = KEEP_WORKING;		
}



CThread::~CThread()
{	
	if(this->m_pThread != NULL )
	{
		this->m_pThread->ExitInstance();
	}

}

void CThread::stop()
{
	this->m_flag = STOP_WORKING;
	this->m_pThread->ResumeThread();

}
//Thread creation function

UINT CThread::ThreadFunction(LPVOID pParam)
{
	CSingleLock singleLock(&m_threadCS);			
	CThread *pThread = (CThread *)pParam;
	pThread->m_flag =1;
	::Sleep(1);
	//pThread->mp_thread->SuspendThread();
	while(pThread->m_flag !=STOP_WORKING )
	{
		//Do the work if the target is not null;
		if(pThread->m_pTarget !=  NULL)
		{
			pThread->m_pTarget->run();
			//If auto delete set in the target then destroy the target
			if(pThread->m_pTarget->AutoDelete())
				delete pThread->m_pTarget;			
			pThread->m_pTarget = NULL;			
		}
		//Add the free thread to the pool
		if(pThread->m_pPool == NULL) break;
		singleLock.Lock();
		if(pThread->m_pPool->getCurPoolSize() <= pThread->m_pPool->getMaxFreePoolSize())
		{
			pThread->m_pPool->addFreeThread(pThread);	
			//As the current work is complete so suspend the thread.
			singleLock.Unlock();
			pThread->m_pThread->SuspendThread();			
		}
		else
		{
			//delete this thread as already the max size limit is crossed
			pThread->stop();
			singleLock.Unlock();
		}		
	}
	//If the pool is not null then call delete the thread.
	if(pThread->m_pPool != NULL) 
	{
		pThread->m_pPool->deleteThread(pThread);
	}
	else 
	{    //As the thread pThread does not  belong to any pool then delete the thread here as 
		 //the POOL can not clear this memory
		delete pThread;
	}		
	return 0;
}


BOOL CThread::isFree()
{
	if(this->m_pTarget == NULL)
	{
		return TRUE;
	}
	else
	{
		return FALSE;
	}
}


void CThread::setTarget(CThTarget *pTarget)
{
	this->m_pTarget = pTarget;
	//Now the thread should be resumed to process the target
	::Sleep(0);
	this->m_pThread->ResumeThread();
	
}

void CThread::setPool(CThPool *pPool)
{
	this->m_pPool = NULL;
}

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.

License

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


Written By
Architect
United States United States
I am a fun loving person and believe in "Work With Fun".
I love to code, design, and architect. Although I worked on several programming languages but I love to work with OOPL like C++ and Java.

Comments and Discussions