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\n",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.\n");
54 getchar();
55 }

 

windows系统调用 线程创建,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。