Thread Problems
Found this from MSDN->codeguru, can't seem to figure out the problem....
class ThreadClass {
protected:
HANDLE m_hThread; //Thread handle
bool m_bActive; //activity indicator
DWORD m_lpId; //Thread ID
public:
ThreadClass(){}
virtual ~ThreadClass(){Kill();}
//Thread Management
bool CreateNewThread() {
m_hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)_ThreadFunc,
(LPVOID) this, 0, (LPDWORD) &m_lpId);
if (m_hThread == NULL) return false;
m_bActive = true;
return true;
};
//Wait for thread to end
bool Wait() {return (WAIT_OBJECT_0 == WaitForSingleObject(m_hThread,
INFINITE));}
bool Suspend(){
m_bActive = false;
return(-1 != SuspendThread(m_hThread));
};
//Suspend the thread
bool Resume() {
m_bActive = true;
return(-1 != ResumeThread(m_hThread));
};
//Resume a suspended thread
bool Kill() {return TerminateThread(m_hThread, 1);}
//Terminate a thread
bool IsActive() {return m_bActive;}
//Check for activity
//override these functions in the derived class
virtual void ThreadEntry(){ }
virtual void ThreadExit(){ }
virtual void Run(){ }
//a friend
friend DWORD WINAPI _ThreadFunc(LPVOID pvThread);
};
DWORD WINAPI _ThreadFunc(LPVOID pvThread) {
ThreadClass* pThread = (ThreadClass*)pvThread; //typecast
pThread->ThreadEntry(); //initialize
pThread->Run();
pThread->ThreadExit(); //finalize
return 0;
};
class MyThread :: public ThreadClass
{
virtual void ThreadEntry()
{
//Initialize
}
virtual void ThreadExit()
{
//Finalize
}
virtual void Run()
{
//do something
}
};