#include <windows.h>
#include <stdio.h>
class CworkerThread
{
protected:
HANDLE m_hThread;
LPCTSTR m_szName;
public:
CworkerThread(LPCTSTR szName)
{
m_szName=szName;
m_hThread=INVALID_HANDLE_VALUE;
m_hThread=::CreateThread(NULL,0,ThreadProc,reinterpret_cast<LPVOID>(this),0,NULL);
}
virtual ~CworkerThread()
{
::CloseHandle(m_hThread);
}
virtual void WaitForCompletion()
{
::WaitForSingleObject(m_hThread,INFINITE);
}
protected:
static DWORD WINAPI ThreadProc(LPVOID lpParam)
{
CworkerThread *pThis=reinterpret_cast<CworkerThread *>(lpParam);
pThis->DoStuff();
return (0);
}
virtual void DoStuff() //运行的结果显示这个函数只调用一次,为什么会这样呢?
{
for(int n=0;n<20;++n)
{
printf("Thread: %s\n",m_szName);
printf("ID: %d\n",::GetCurrentThreadId());
printf("count: %d\n\n",n);
}
}
};
void main()
{
CworkerThread wtA("A");
CworkerThread wtB("B");
wtA.WaitForCompletion();
wtB.WaitForCompletion();
printf("Both threads complete.");
}
问题已经用红色字体标识了,期待高手的解答~~~QQ:304635945