一、问题提出: 利用多线程原理实现打印票据 二、问题实现: // 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; } 此程序会存在一些潜在的问题,下面将出提出这个问题。