在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

转载于:https://www.cnblogs.com/zhangpengshou/archive/2010/03/29/1699900.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值