Thread base class

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Folks,
For a while now I have been using threads by deriving them from a
"modified" version of the <a target="_blank"
href="http://www.codeproject.com/threads/ag_thread.asp";>Win32 Worker
Thread Wrapper class (by Amer Gerzic)</a> that performs the repetitive
tasks of thread creation (etc) so that all the derived class has to do
is to call StartThread() and then override a virtual method Main()
where the thread does its work.

Reading around, however, I have found that if using MFC in the
derived class, the thread should be created with AfxBeginThread(...)
rather than with CreateThread(...), which is what this class does.

So I have modified the base class and have come up with the following
(below) which seems to work fine. Since my knowledge of threads is
borderline dismal, my questions to you guys are: 1. Are there any
blatant blunders? 2. Any subtle ones? 3. What should be added to make
it more robust/useful while keeping it as simple as possible? 4. And,
have I complicated something that could be done better?

class CTHR_BaseThread
{
public:
CTHR_BaseThread()
{
m_hThread = NULL;
m_bInYourOwnTime = FALSE;
m_bRunning = FALSE;
}

// This function is used only to call the virtual function (which
must be overridden).
// In this way all non-static members can be used from within the
thread.
static UINT StaticEntryPoint( LPVOID lParam )
{
CTHR_BaseThread* pThis = static_cast<CTHR_BaseThread*>(lParam);

UINT nRet = 0;

if( pThis != NULL )
{
pThis->m_bRunning = TRUE;
nRet = pThis->ThreadMain();
pThis->m_bRunning = FALSE;
}

return nRet;
}

// Operations
public:
// Call this method from the derived class to start the thread
BOOL StartThread()
{
// Allows thread to run in case if was aborted before
m_bInYourOwnTime = FALSE;

// If the thread is already running do not start another one
if( IsThreadRunning() )
return FALSE;

m_hThread = AfxBeginThread( StaticEntryPoint, this );

if( m_hThread == NULL )
return FALSE;

return TRUE;
}

BOOL IsThreadRunning()
{
return m_bRunning;
}

void InYourOwnTime()
{
m_bInYourOwnTime = TRUE;
}

protected:
// OVERRIDE THIS METHOD IN DERIVED CLASS!!
virtual UINT ThreadMain() = 0;

// Attributes
protected:
// Thread handle
CWinThread* m_hThread;

BOOL m_bRunning;
BOOL m_bInYourOwnTime;

public:
virtual ~CTHR_BaseThread()
{
InYourOwnTime();
}
};

Thank you all and be well.

.



Relevant Pages

  • Re: Wie programmiert man den Systray?
    ... CSystemTray::CSystemTray(HINSTANCE hInst, HWND hWnd, UINT uCallbackMessage, ... BOOL CSystemTray::RemoveIcon ... // variables in TimerFuncvia a reinterpret_cast in the static CALLBACK ...
    (microsoft.public.de.vc)
  • _fcloseall () conflicts with DirectMusic 8.1
    ... MusicManager::SetBGMusic (LPCTSTR *ppFile, UINT nFile, UINT uVol, UINT ... uChangeTime, BOOL bFade) ... MusicManager::CreateSegmentFromFile (Music **ppMusic, LPCTSTR szFile, BOOL ... DXUtil_ConvertGenericStringToWide (wszFile, szFile); ...
    (microsoft.public.win32.programmer.directx.audio)
  • Re: CryptUIDlgViewCertificate: hows the signature?
    ... Gabriele ... > public static extern bool CryptUIDlgViewCertificate( ... > public uint dwFlags; ... > public IntPtr rgszPurposes; ...
    (microsoft.public.platformsdk.security)
  • [PATCH 23/44] Use __obsolete_setup() in input drivers to warn about obsolete kernel params
    ... module_param_named(softrepeat, atkbd_softrepeat, bool, 0); ... module_param_named(irq, pc98bm_irq, uint, 0); ... static int inport_used; ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • [PATCH] Remove MODULE_PARM from i386 defconfig.
    ... This cleans up defconfig for i386. ... +module_param(tzp, int, 0); ... +module_param(autoclose, bool, 0); ... +module_param_array(frame_len, uint, NULL, 0); ...
    (Linux-Kernel)