15,471
社区成员
发帖
与我相关
我的任务
分享
#include "stdafx.h"
#include "Thread.h"
CThread::CThread(IRunnable * run)
{
m_run = run;
}
CThread::~CThread()
{
::CloseHandle(m_hThread);
}
static DWORD WINAPI ThreadFunc(LPVOID param)
{
CThread* thread = (CThread*)param;
if (thread->m_run != NULL)
{
thread->m_run->Run();
}
return 0;
}
void CThread::Start()
{
if (m_hThread == NULL)
{
DWORD dwThreadID = 0;
m_hThread = ::CreateThread(NULL, 0, ThreadFunc, this, 0, &dwThreadID);
}
}
void CThread::WaitFinish()
{
::WaitForSingleObject(m_hThread, INFINITE);
}
class IRunnable
{
public:
virtual void Run() = 0;
};
class CThread
{
public:
CThread(IRunnable * run);
~CThread();
public:
void Start();
void WaitFinish();
public:
IRunnable* m_run = NULL;
HANDLE m_hThread = NULL;
};
楼主有了解回调和虚函数吗?
有的话,上面的接口就可以办到说的。