參考文章:
http://blog.csdn.net/wei801004/archive/2006/05/18/744341.aspx
http://blog.csdn.net/SeaWave/archive/2006/05/20/746399.aspx
http://www.shnenglu.com/Files/bujiwu/CThread.rar
http://blog.csdn.net/wei801004/archive/2006/05/18/744341.aspx
http://blog.csdn.net/SeaWave/archive/2006/05/20/746399.aspx
1 #include <iostream>
2 #include <Windows.h>
3 #include <process.h>
4 #include <conio.h>
5
6 using namespace std;
7
8 class CThread
9 {
10 public:
11 CThread(void)
12 {
13 m_running = true;
14 }
15
16 virtual ~CThread(void)
17 {
18 }
19
20 void StartThread(void);
21 void StopThread(void);
22
23 void WaitForEnd(DWORD dwTimeOut = INFINITE);
24
25 virtual DWORD WINAPI ThreadWorkItem(LPVOID lpParameter) = 0;
26
27 private:
28 static DWORD WINAPI ThreadFun(LPVOID lpParameter);
29
30 protected:
31 bool m_running;
32
33 private:
34 HANDLE m_hThread;
35 };
36
37 inline void CThread::StartThread(void)
38 {
39 m_hThread = CreateThread(NULL, 0, ThreadFun, this, 0, 0);
40
41 if (m_hThread == NULL)
42 {
43 throw exception("Error: Start thread!");
44 }
45
46 CloseHandle(m_hThread);
47
48 return;
49 }
50
51 inline void CThread::WaitForEnd(DWORD dwTimeOut /* = INFINITE */)
52 {
53 DWORD dwRet = WaitForSingleObject(m_hThread, dwTimeOut);
54
55 if (dwRet == WAIT_OBJECT_0)
56 {
57 CloseHandle(m_hThread);
58 m_hThread = NULL;
59 }
60 else if (dwRet == WAIT_TIMEOUT)
61 {
62 throw exception("Error: Time out!");
63 }
64
65 return;
66 }
67
68 inline void CThread::StopThread()
69 {
70 m_running = false;
71 }
72
73 inline DWORD WINAPI CThread::ThreadFun(LPVOID lpParameter)
74 {
75 CThread *pThread = reinterpret_cast<CThread*> (lpParameter);
76 pThread->ThreadWorkItem(lpParameter);
77
78 return 0;
79 }
80
81 class SubThread : public CThread
82 {
83 public:
84 SubThread(int Num)
85 {
86 Start = Num;
87 }
88
89 DWORD WINAPI ThreadWorkItem(LPVOID lpParameter)
90 {
91 while ( m_running )
92 {
93 cout << "線程執行的操作[" << Start++ << "]" <<endl;
94 Sleep(1000);
95 }
96
97 ExitThread(0);
98
99 return 0;
100 }
101
102 private:
103 int Start;
104 };
105
106 int main(int argc, char* argv[])
107 {
108 SubThread MyThead(1);
109
110 cout << "To exit this server, hit a key at any time on this console
" <<endl;
111
112 MyThead.StartThread();
113 //MyThead.WaitForEnd();
114
115 while(!_kbhit())
116 {
117 Sleep(0);
118 }
119
120 MyThead.StopThread();
121
122 return 0;
123 }
124
2 #include <Windows.h>
3 #include <process.h>
4 #include <conio.h>
5
6 using namespace std;
7
8 class CThread
9 {
10 public:
11 CThread(void)
12 {
13 m_running = true;
14 }
15
16 virtual ~CThread(void)
17 {
18 }
19
20 void StartThread(void);
21 void StopThread(void);
22
23 void WaitForEnd(DWORD dwTimeOut = INFINITE);
24
25 virtual DWORD WINAPI ThreadWorkItem(LPVOID lpParameter) = 0;
26
27 private:
28 static DWORD WINAPI ThreadFun(LPVOID lpParameter);
29
30 protected:
31 bool m_running;
32
33 private:
34 HANDLE m_hThread;
35 };
36
37 inline void CThread::StartThread(void)
38 {
39 m_hThread = CreateThread(NULL, 0, ThreadFun, this, 0, 0);
40
41 if (m_hThread == NULL)
42 {
43 throw exception("Error: Start thread!");
44 }
45
46 CloseHandle(m_hThread);
47
48 return;
49 }
50
51 inline void CThread::WaitForEnd(DWORD dwTimeOut /* = INFINITE */)
52 {
53 DWORD dwRet = WaitForSingleObject(m_hThread, dwTimeOut);
54
55 if (dwRet == WAIT_OBJECT_0)
56 {
57 CloseHandle(m_hThread);
58 m_hThread = NULL;
59 }
60 else if (dwRet == WAIT_TIMEOUT)
61 {
62 throw exception("Error: Time out!");
63 }
64
65 return;
66 }
67
68 inline void CThread::StopThread()
69 {
70 m_running = false;
71 }
72
73 inline DWORD WINAPI CThread::ThreadFun(LPVOID lpParameter)
74 {
75 CThread *pThread = reinterpret_cast<CThread*> (lpParameter);
76 pThread->ThreadWorkItem(lpParameter);
77
78 return 0;
79 }
80
81 class SubThread : public CThread
82 {
83 public:
84 SubThread(int Num)
85 {
86 Start = Num;
87 }
88
89 DWORD WINAPI ThreadWorkItem(LPVOID lpParameter)
90 {
91 while ( m_running )
92 {
93 cout << "線程執行的操作[" << Start++ << "]" <<endl;
94 Sleep(1000);
95 }
96
97 ExitThread(0);
98
99 return 0;
100 }
101
102 private:
103 int Start;
104 };
105
106 int main(int argc, char* argv[])
107 {
108 SubThread MyThead(1);
109
110 cout << "To exit this server, hit a key at any time on this console

111
112 MyThead.StartThread();
113 //MyThead.WaitForEnd();
114
115 while(!_kbhit())
116 {
117 Sleep(0);
118 }
119
120 MyThead.StopThread();
121
122 return 0;
123 }
124
http://www.shnenglu.com/Files/bujiwu/CThread.rar