码迷,mamicode.com
首页 > 编程语言 > 详细

windows系统调用 线程创建

时间:2014-06-03 08:57:14      阅读:257      评论:0      收藏:0      [点我收藏+]

标签:c   style   class   blog   code   java   

bubuko.com,布布扣
 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 }
bubuko.com,布布扣

 

windows系统调用 线程创建,布布扣,bubuko.com

windows系统调用 线程创建

标签:c   style   class   blog   code   java   

原文地址:http://www.cnblogs.com/593213556wuyubao/p/3760586.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!