Monday, January 26, 2009

Thread Class for C++

1. CBaseThread.h

#pragma once
const DWORD TERMINATION_TIMEOUT = 10000;


class CBaseThread
{
protected:
DWORD m_dwThreadId;
HANDLE m_hTerminateEvent;
HANDLE m_hThread;

protected:
virtual DWORD ThreadProcedure(){return 0;};

public:
bool SetTerminationEvent(HANDLE hTerminationEvent);
bool ShouldTerminateThread();
virtual void TerminateThread();
static DWORD WINAPI ThreadProcedure(PVOID pVoid);
CBaseThread();
virtual ~CBaseThread();
};


2. CBaseThread.cpp

#include "stdafx.h"
#include "CBaseThread.h"

//////////////////////////////
////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CBaseThread::CBaseThread()
{
m_dwThreadId = NULL;
m_hTerminateEvent = NULL;
m_hThread = NULL;
m_hTerminateEvent = CreateEvent(NULL,TRUE,FALSE,NULL);
ResetEvent(m_hTerminateEvent);
}
CBaseThread::~CBaseThread()
{
CloseHandle(m_hTerminateEvent);
}

//> ---------------------------------------------------------------------------
//> Function: CBaseThread::ThreadProcedure
//> Params: [in] void *pVoid data passed to the thread procedure
//> Returns: DWORD
//> Purpose: implements the static CBaseThread derived class thread procedure
//> by calling the derived class ThreadProcedure method
//> ---------------------------------------------------------------------------
DWORD CBaseThread::ThreadProcedure(void *pVoid)
{
CBaseThread *pThread = static_cast(pVoid);

return pThread->ThreadProcedure();
}
//> ---------------------------------------------------------------------------
//> Function: CBaseThread::TerminateThread
//> Params: none
//> Returns: void
//> Purpose: call this method to terminate the thread procedure
//> ---------------------------------------------------------------------------
void CBaseThread::TerminateThread()
{
// Signal the thread to terminate itself
SetEvent(m_hTerminateEvent);

// wait for the thread procedure to exit before returning
WaitForSingleObject(m_hThread,TERMINATION_TIMEOUT);

if (NULL != m_hThread)
{
CloseHandle(m_hThread);
}
}
//> ---------------------------------------------------------------------------
//> Function: CBaseThread::ShouldTerminateThread
//> Params: void
//> Returns: bool
//> Purpose: check to see if thread termination was requested
//> ---------------------------------------------------------------------------
bool CBaseThread::ShouldTerminateThread()
{
bool bReturn = false;
if (WAIT_OBJECT_0 == WaitForSingleObject(m_hTerminateEvent,0))
{
bReturn = true;
}
return bReturn;
}

3. Usage:

3.1. CDeviceDiscoveryThread.h

#pragma once
#include "cbasethread.h"
#include
#include
#include
#include
class CGigaBluetooth;
class CDeviceDiscoveryThread : public CBaseThread
{
private:
CGigaBluetooth *m_pCMSBluetooth;
protected:
virtual DWORD ThreadProcedure();

public:
static CDeviceDiscoveryThread *ThreadFactory(CGigaBluetooth *pCMSBluetooth);
CDeviceDiscoveryThread(CGigaBluetooth *pCMSBluetooth);
virtual ~CDeviceDiscoveryThread(void);
};

3.2. CDeviceDiscoveryThread.cpp

#include "StdAfx.h"
#include "GigaBluetooth.h"
#include "DeviceDiscoveryThread.h"

CDeviceDiscoveryThread::CDeviceDiscoveryThread(CGigaBluetooth *pCMSBluetooth)
{
ASSERT(NULL != pCMSBluetooth);
m_pCMSBluetooth = pCMSBluetooth;
}

CDeviceDiscoveryThread::~CDeviceDiscoveryThread(void)
{
}
//> ---------------------------------------------------------------------------
//> Function: ThreadFactory
//> Params: MSBluetooth *pMSBluetooth
//> Returns: CDeviceDiscoveryThread *
//> Purpose: creates CDeviceDiscoveryThread threads
//> ---------------------------------------------------------------------------
CDeviceDiscoveryThread * CDeviceDiscoveryThread::ThreadFactory(CGigaBluetooth *pCMSBluetooth)
{
CDeviceDiscoveryThread *pCDeviceDiscoveryThread = new CDeviceDiscoveryThread(pCMSBluetooth);

if (NULL != pCDeviceDiscoveryThread)
{
pCDeviceDiscoveryThread->m_hThread = ::CreateThread(NULL,0,CBaseThread::ThreadProcedure,pCDeviceDiscoveryThread,0,&pCDeviceDiscoveryThread->m_dwThreadId);
}
return pCDeviceDiscoveryThread;
}

//> ---------------------------------------------------------------------------
//> Function: ThreadProcedure
//> Params: none
//> Returns: DWORD
//> Purpose: implements the thread procedure
//> ---------------------------------------------------------------------------
DWORD CDeviceDiscoveryThread::ThreadProcedure()
{
Sleep(5000); //test
CMessages::PostMessage(DISCOVERY_COMPLETE);
return 0;
}

3.3.

CDeviceDiscoveryThread *m_pCDeviceDiscoveryThread;
m_pCDeviceDiscoveryThread = CDeviceDiscoveryThread::ThreadFactory(this);
return NULL != m_pCDeviceDiscoveryThread ? true : false;

No comments: