typedef unsigned (WINAPI *PTHREADFUN)(LPVOID lpParameter);
class CThread
{
public:
CThread(void) {}
~CThread(void) {}
void Start(void);
void Stop(void) const { CloseHandle(m_hThread); }
static DWORD WINAPI ThreadFun(LPVOID lpParameter);
virtual DWORD WINAPI ThreadWorkItem(LPVOID lpParameter) = 0;
private:
HANDLE m_hThread;
DWORD m_dwThreadID;
};
inline void CThread::Start(void)
{
m_hThread = reinterpret_cast<HANDLE>(_beginthreadex(NULL, NULL, (PTHREADFUN) ThreadFun, this, 0, (unsigned*) &m_dwThreadID));
if (m_hThread == NULL)
{
throw Exception("Error: Start thread!");
}
}
inline DWORD WINAPI CThread::ThreadFun(LPVOID lpParameter)
{
CThread *pThread = reinterpret_cast<CThread*> (lpParameter);
pThread->ThreadWorkItem(lpParameter);
return 0;
}
只要继承上面的类,然后重写ThreadWorkItem(LPVOID)这个函数就可以了。
class test : public CThread
{
public:
test(void) {}
~test(void) {}
DWORD WINAPI ThreadWorkItem(LPVOID lpParameter){ cout<<"线程执行的操作"<<endl; }
};