C++多线程:My First C++ Thread

来源:互联网 发布:淘宝违规处罚节点 编辑:程序博客网 时间:2024/05/17 03:46

// mm.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "mm.h"

 

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

 

DWORD WINAPI ThreadProc(LPVOID lpParam)
{
 int i = 0;
 while(i < 20)
 {
  printf("I am from a thread,count = %d/n",i++);
 }
 return 0;
}

 

// The one and only application object

CWinApp theApp;

using namespace std;

 

int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
 int nRetCode = 0;

 // initialize MFC and print and error on failure


 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
 {
  // TODO: change error code to suit your needs
  _tprintf(_T("Fatal Error: MFC initialization failed/n"));
  nRetCode = 1;
 }


 else
 {
  HANDLE hThread;
 DWORD dwThreadId;
 hThread = CreateThread(
  NULL,   
  NULL,   
  ThreadProc,  
  NULL,  
  0,    
  &dwThreadId  
  );
 printf("Now another thread has been Created,ID:%d/n",dwThreadId);


  WaitForSingleObject(hThread,INFINITE); 

 CloseHandle(hThread);
 cin.get();

 }

 return nRetCode;
}

原创粉丝点击