下面是在重载了MFC中的线程操作类,这样的操作是线程安全的
#include <afxwin.h>
#include <iostream.h>
class CUserThread : public CWinThread
{
public:
CUserThread(AFX_THREADPROC ThreadProc);
static UINT ThreadFunc(LPVOID lparam);
public:
int startcounter ;
private:
virtual void GO();
};
CUserThread::CUserThread(AFX_THREADPROC ThreadProc):CWinThread(ThreadProc , NULL)
{
m_bAutoDelete = false ;
m_pThreadParams = this ;
}
UINT CUserThread::ThreadFunc(LPVOID lparams)
{
CUserThread * userThread = (CUserThread *) lparams ;
userThread->GO();
return 0 ;
}
void CUserThread::GO()
{
for(int i=0;i<10 ;i++)
{
printf("%d\n" , i);
Sleep(1000);
}
}
void main()
{
CUserThread * pUserThread[5];
for(int i=0;i<5;i++)
{
pUserThread[i] = new CUserThread(CUserThread::ThreadFunc);
pUserThread[i]->CreateThread();
}
for(int j=0;i<5;i++)
{
WaitForSingleObject(pUserThread[j]->m_hThread , INFINITE);
delete pUserThread[j] ;
}
}