上一篇? 代碼有問題,經過兩次重構, 88 line 代碼
測試代碼::
實現代碼::
測試代碼::
?1
?2
#include?"thread.h"
?3
?4
class?testThread
?5

{
?6
public:
?7
????testThread()
?8
????
{
?9
????????m_Obj.setObj(test);
10
????????
11
????}
12
13
????virtual?~testThread()
14
????
{
15
????????m_Obj.stop();
16
????}
17
18
????static?unsigned?int?test(void*?pVoid)
19
????
{
20
????????testThread*?pThis?=?(testThread*)pVoid;
21
22
????????::EnterCriticalSection(&pThis->m_cs);
23
????????cout?<<?"test"?<<?endl;
24
????????::LeaveCriticalSection(&pThis->m_cs);
25
????????return?0;
26
????}
27
28
29
????void?go()
30
????
{
31
????????m_Obj.start(this);
32
????}
33
34
35
????CThread?m_Obj;
36
????static?CRITICAL_SECTION?m_cs;
37
};
38
39
CRITICAL_SECTION?testThread::m_cs;
40
41
int?main()
42

{????
43
????::InitializeCriticalSection(&testThread.m_cs);
44
45
????
{
46
????????testThread?B[100000];
47
????????
48
????????for(?int?i?=?0;?i?<?100000;?i++?)
49
????????
{
50
????????????B[i].go();
51
????????}
52
????}
53
54
????::DeleteCriticalSection(&testThread.m_cs);
55
56
????return?0;
57
}

?2
#include?"thread.h"?3

?4
class?testThread?5


{?6
public:?7
????testThread()?8

????
{?9
????????m_Obj.setObj(test);10
????????11
????}12

13
????virtual?~testThread()14

????
{15
????????m_Obj.stop();16
????}17

18
????static?unsigned?int?test(void*?pVoid)19

????
{20
????????testThread*?pThis?=?(testThread*)pVoid;21

22
????????::EnterCriticalSection(&pThis->m_cs);23
????????cout?<<?"test"?<<?endl;24
????????::LeaveCriticalSection(&pThis->m_cs);25
????????return?0;26
????}27

28

29
????void?go()30

????
{31
????????m_Obj.start(this);32
????}33

34

35
????CThread?m_Obj;36
????static?CRITICAL_SECTION?m_cs;37
};38

39
CRITICAL_SECTION?testThread::m_cs;40

41
int?main()42


{????43
????::InitializeCriticalSection(&testThread.m_cs);44

45

????
{46
????????testThread?B[100000];47
????????48
????????for(?int?i?=?0;?i?<?100000;?i++?)49

????????
{50
????????????B[i].go();51
????????}52
????}53

54
????::DeleteCriticalSection(&testThread.m_cs);55

56
????return?0;57
}實現代碼::
?1
#include?<windows.h>
?2
#include?<process.h>
?3
?4
class?CThread
?5

{
?6
public:
?7
????CThread(unsigned?int?(*pfnCall)(void*?pVoid))
?8
????
{
?9
????????m_pfnCall???=?pfnCall;
10
????????m_hThread???=?INVALID_HANDLE_VALUE;
11
????????m_nThreadID?=?0;
12
????????m_pObj??????=?NULL;
13
????}
14
15
????virtual?~CThread()
16
????
{
17
????????stop();
18
????}
19
20
????static?unsigned?__stdcall?_ThreadProc(void*?pVoid)
21
????
{
22
????????CThread*?pThis?=?(CThread*)pVoid;
23
24
????????if?(?NULL?!=?pThis->m_pfnCall?)
25
????????????pThis->m_pfnCall(pThis->m_pObj);
26
27
?????????_endthread();
28
29
????????return?0;
30
????}
31
32
????bool?start(void*?pVoid)
33
????
{
34
????????m_pObj?=?pVoid;
35
????????m_hThread?=?(HANDLE)_beginthreadex(NULL,?0,?_ThreadProc,?(void?*)this,?0,?&m_nThreadID);
36
????????
37
????????if?(?0?==?m_hThread?)
38
????????????return?false;
39
????????else
40
????????????return?true;
41
????}
42
43
????bool?stop()
44
????
{
45
????????#define?ONE_SECOND?1000L
46
47
????????if?(?INVALID_HANDLE_VALUE?==?m_hThread?)
48
????????????return?true;
49
50
????????BOOL?bRet???=?FALSE;
51
????????DWORD?dwRet?=?0;
52
53
????????DWORD?dwExitCode?=?0;
54
????????if(?TRUE?==?::GetExitCodeThread(m_hThread,?&dwExitCode)?)
55
????????
{
56
????????????if(?STILL_ACTIVE?==?dwExitCode?)????????????
57
????????????????dwRet?=?::WaitForSingleObject(m_hThread,?ONE_SECOND);?//?INFINITE
58
????????}
59
????????????????
60
????????if?(?dwRet?==?WAIT_TIMEOUT?||?dwRet?==?WAIT_FAILED?)?
61
????????
{
62
????????????bRet?=?::TerminateThread(m_hThread,?1);
63
????????}
64
????????else
65
????????
{
66
????????????bRet?=?TRUE;
67
????????}
68
????????
69
????????if?(?TRUE?==?bRet?)
70
????????
{
71
????????????return?true;
72
????????}
73
????????else
74
????????
{
75
????????????return?false;
76
????????}
77
????}????
78
79
private:
80
????CThread()
81
????
{
82
????}
83
84
????HANDLE???????m_hThread;
85
????unsigned?int?m_nThreadID;
86
????unsigned?int?(*m_pfnCall)(void*?pVoid);
87
????void*????????m_pObj;
88
};
#include?<windows.h>?2
#include?<process.h>?3

?4
class?CThread?5


{?6
public:?7
????CThread(unsigned?int?(*pfnCall)(void*?pVoid))?8

????
{?9
????????m_pfnCall???=?pfnCall;10
????????m_hThread???=?INVALID_HANDLE_VALUE;11
????????m_nThreadID?=?0;12
????????m_pObj??????=?NULL;13
????}14

15
????virtual?~CThread()16

????
{17
????????stop();18
????}19

20
????static?unsigned?__stdcall?_ThreadProc(void*?pVoid)21

????
{22
????????CThread*?pThis?=?(CThread*)pVoid;23

24
????????if?(?NULL?!=?pThis->m_pfnCall?)25
????????????pThis->m_pfnCall(pThis->m_pObj);26

27
?????????_endthread();28

29
????????return?0;30
????}31

32
????bool?start(void*?pVoid)33

????
{34
????????m_pObj?=?pVoid;35
????????m_hThread?=?(HANDLE)_beginthreadex(NULL,?0,?_ThreadProc,?(void?*)this,?0,?&m_nThreadID);36
????????37
????????if?(?0?==?m_hThread?)38
????????????return?false;39
????????else40
????????????return?true;41
????}42

43
????bool?stop()44

????
{45
????????#define?ONE_SECOND?1000L46

47
????????if?(?INVALID_HANDLE_VALUE?==?m_hThread?)48
????????????return?true;49

50
????????BOOL?bRet???=?FALSE;51
????????DWORD?dwRet?=?0;52

53
????????DWORD?dwExitCode?=?0;54
????????if(?TRUE?==?::GetExitCodeThread(m_hThread,?&dwExitCode)?)55

????????
{56
????????????if(?STILL_ACTIVE?==?dwExitCode?)????????????57
????????????????dwRet?=?::WaitForSingleObject(m_hThread,?ONE_SECOND);?//?INFINITE58
????????}59
????????????????60
????????if?(?dwRet?==?WAIT_TIMEOUT?||?dwRet?==?WAIT_FAILED?)?61

????????
{62
????????????bRet?=?::TerminateThread(m_hThread,?1);63
????????}64
????????else65

????????
{66
????????????bRet?=?TRUE;67
????????}68
????????69
????????if?(?TRUE?==?bRet?)70

????????
{71
????????????return?true;72
????????}73
????????else74

????????
{75
????????????return?false;76
????????}77
????}????78

79
private:80
????CThread()81

????
{82
????}83

84
????HANDLE???????m_hThread;85
????unsigned?int?m_nThreadID;86
????unsigned?int?(*m_pfnCall)(void*?pVoid);87
????void*????????m_pObj;88
};

