您的位置:首页 > 编程语言

VC多线程编程一(创建多线程)

2010-10-08 09:43 375 查看
一、问题提出:

利用多线程原理实现打印票据

二、问题实现:

// ManyThread.cpp : Defines the entry point for the console application.
/*
Auth : Jet
Date : 2010.10.7
Fuction : Print Ticket.
*/

#include "stdafx.h"
#include "ManyThread.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif

// The one and only application object
CWinApp theApp;
using namespace std;
int ticket=200;		//Definition global variable .

DWORD WINAPI ThreadProc1( LPVOID lpParameter)		//Thread one
{
while(1)
{
if(ticket>0)
{
printf("tickte1=%d/n",ticket);			//print
ticket--;
}
else
break;
}
return true;
}

DWORD WINAPI ThreadProc2( LPVOID lpParameter)		//Thread two
{
while(1)
{
if(ticket>0)
{
printf("tickte2=%d/n",ticket);
ticket--;
}
else
break;
}
return true;
}

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))
{
_tprintf(_T("Fatal Error: MFC initialization failed/n"));
nRetCode = 1;
}
else
{
// TODO: code your application's behavior here.

HANDLE thread1,thread2;
thread1=CreateThread(NULL,0,ThreadProc1,0,0,NULL);		//Create Thread one
thread2=CreateThread(NULL,0,ThreadProc2,0,0,NULL);		//Create Thread two

CloseHandle(thread1);
}
getchar();
return nRetCode;
}


此程序会存在一些潜在的问题,下面将出提出这个问题。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: