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

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-14
  • 2021-09-15
  • 2022-12-23
  • 2021-09-07
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-10
  • 2022-12-23
  • 2021-08-08
  • 2022-12-23
  • 2021-11-17
  • 2021-12-20
相关资源
相似解决方案