这个例子来自MSDN
1 // crt_begthrdex.cpp
2 // compile with: /MT
3 #include <windows.h>
4 #include <stdio.h>
5 #include <process.h>
6
7 unsigned Counter;
8 unsigned __stdcall SecondThreadFunc( void* pArguments )
9 {
10 printf( "In second thread...\n" );
11
12 while ( Counter < 1000000 )
13 Counter++;
14
15 _endthreadex( 0 );
16 return 0;
17 }
18
19 int main()
20 {
21 HANDLE hThread;
22 unsigned threadID;
23
24 printf( "Creating second thread...\n" );
25
26 // Create the second thread.
27 hThread = (HANDLE)_beginthreadex( NULL, 0, &SecondThreadFunc, NULL, 0, &threadID );
28
29 // Wait until second thread terminates. If you comment out the line
30 // below, Counter will not be correct because the thread has not
31 // terminated, and Counter most likely has not been incremented to
32 // 1000000 yet.
33 WaitForSingleObject( hThread, INFINITE );
34 printf( "Counter should be 1000000; it is-> %d\n", Counter );
35 // Destroy the thread object.
36 CloseHandle( hThread );
37 }
没有评论:
发表评论