在VC中使用线程

下面是在重载了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] ;
  }

}

转自:http://zhouweigang01.blog.163.com/blog/#m=0&t=1&c=fks_083066081085080067082083074074087082081074084081

你可能感兴趣的:(线程)