多线程

_beginthreadex(...): 创建并且可以开启一个线程。

_endthreadex(...): 回收线程资源,结束线程。

// multithread.cpp : Defines the entry point for the console application. // #include "stdafx.h" //compile with: /MT #include <windows.h> #include <stdio.h> #include <process.h> unsigned Counter = 0; unsigned __stdcall SecondThreadFunc( void * pArguments ) { printf("In second thread.../n"); while( Counter < 10000000 ) Counter++; _endthreadex( 0 ); return 0; } int _tmain(int argc, _TCHAR* argv[]) { HANDLE hThread; unsigned threadID; printf("Creating second thread.../n"); //Create the second thread. hThread = (HANDLE)_beginthreadex(NULL, 0, &SecondThreadFunc, NULL, 0, &threadID); //Wait until second thread terminates. If you comment out the line below, //Counter will not be correct because the thread has not //terminated, and Counter most likely has not be incremented to 10000000 yet. WaitForSingleObject(hThread, INFINITE); //Sleep(1000); printf( "Counter should be 10000000;, actually it is -> %d/n", Counter); return 0; }

 

你可能感兴趣的:(多线程)