windows系统调用 线程创建

 1 #include "windows.h"
 2 #include "iostream"
 3 using namespace std;
 4 
 5 class CWorkerThread{
 6 public:
 7     CWorkerThread(LPCTSTR szName):
 8       m_szName(szName),m_hThread(INVALID_HANDLE_VALUE){
 9       m_hThread=CreateThread(
10           NULL,
11           0,
12           ThreadProc,
13           reinterpret_cast<LPVOID>(this),
14           0,
15           NULL
16           );
17       }
18 
19       virtual ~CWorkerThread(){
20         CloseHandle(m_hThread);
21       }
22 
23       virtual void WaitForCompletion(){
24         WaitForSingleObject(m_hThread,INFINITE);        
25       }
26 
27 protected:
28     static DWORD WINAPI ThreadProc(LPVOID lpParam){
29         CWorkerThread *pThis=reinterpret_cast<CWorkerThread *>( lpParam);
30         pThis->DoStuff();
31         return 0;
32     }
33 
34 
35     virtual void DoStuff(){
36         for(int n=0;n<10;++n){
37             printf("Thread %s ID:%d,count %d
",m_szName,GetCurrentThreadId(),n);
38         }
39     }
40 
41 protected:
42     HANDLE m_hThread;
43     LPCTSTR m_szName;
44 };
45 
46 void main(){
47 CWorkerThread wtA("A");
48 CWorkerThread wtB("B");
49 
50 wtA.WaitForCompletion();
51 wtB.WaitForCompletion();
52 
53 printf("Both threads complete.
");
54 getchar();
55 }
原文地址:https://www.cnblogs.com/593213556wuyubao/p/3760586.html