class ThreadIf
{
protected:
HANDLE mThread;
Id mId;
public:
ThreadIf();
virtual ~ThreadIf();
public:
virtual void run();
virtual void thread() = 0;
}
extern "C" //定义C函数
{
threadIfThreadWrapper( void* threadParm )
{
assert( threadParm );
ThreadIf* t = static_cast < ThreadIf* > ( threadParm );
assert( t );
t->thread();
return 0;
}
}
ThreadIf::run()
{
mThread =(HANDLE)_beginthreadex (
NULL,
0,
RESIP_THREAD_START_ROUTINE
(threadIfThreadWrapper),
this,
0,
(unsigned*)&mId
);
}
ThreadIf::join()
{
if (mId == 0)
{
return;
}
DWORD exitCode;
while (true)
{
if (GetExitCodeThread(mThread,&exitCode) != 0)
{
if (exitCode != STILL_ACTIVE)
{
break;
}
else
{
WaitForSingleObject(mThread,INFINITE);
}
}
else
{
break;
}
}
// !kh!
CloseHandle(mThread);
mThread=0;
mId = 0;
}
在其派生类中实现thread();