-
Notifications
You must be signed in to change notification settings - Fork 0
/
Thread.h
executable file
·195 lines (171 loc) · 4.92 KB
/
Thread.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#ifndef __THREAD_H__
#define __THREAD_H__
public class CThread
{
public:
/*
* Info: Default Constructor
*/
CThread()
{
m_pThreadFunc = CThread::EntryPoint; // Can call Detach() also.
}
/*
* Info: Plug Constructor
*
* Use this to migrate/port existing worker threads to objects immediately
* Although you lose the benefits of ThreadCTOR and ThreadDTOR.
*/
CThread(LPTHREAD_START_ROUTINE lpExternalRoutine)
{
Attach(lpExternalRoutine);
}
/*
* Info: Default Destructor
*
* I think it is wise to destroy the thread even if it is running,
* when the main thread reaches here.
*/
~CThread()
{
if ( m_ThreadCtx.m_hThread )
Stop(true);
}
/*
* Info: Starts the thread.
*
* This function starts the thread pointed by m_pThreadFunc with default attributes
*/
DWORD Start( void* arg = NULL )
{
m_ThreadCtx.m_pUserData = arg;
m_ThreadCtx.m_hThread = CreateThread(NULL, 0, m_pThreadFunc, this, 0, &m_ThreadCtx.m_dwTID);
m_ThreadCtx.m_dwExitCode = (DWORD)-1;
return GetLastError();
}
/*
* Info: Stops the thread.
*
* This function stops the current thread.
* We can force kill a thread which results in a TerminateThread.
*/
DWORD Stop ( bool bForceKill = false )
{
if ( m_ThreadCtx.m_hThread )
{
GetExitCodeThread(m_ThreadCtx.m_hThread, &m_ThreadCtx.m_dwExitCode);
if ( m_ThreadCtx.m_dwExitCode == STILL_ACTIVE && bForceKill )
TerminateThread(m_ThreadCtx.m_hThread, DWORD(-1));
m_ThreadCtx.m_hThread = NULL;
}
return m_ThreadCtx.m_dwExitCode;
}
/*
* Info: Starts the thread.
*
* This function starts the thread pointed by m_pThreadFunc with default attributes
*/
DWORD GetExitCode() const
{
if ( m_ThreadCtx.m_hThread )
GetExitCodeThread(m_ThreadCtx.m_hThread, (LPDWORD)&m_ThreadCtx.m_dwExitCode);
return m_ThreadCtx.m_dwExitCode;
}
/*
* Info: Attaches a Thread Function
*
* Used primarily for porting but can serve in developing generic thread objects
*/
void Attach( LPTHREAD_START_ROUTINE lpThreadFunc ){
m_pThreadFunc = lpThreadFunc;
}
/*
* Info: Detaches the Attached Thread Function
*
* Detaches the Attached Thread Function, If any.
* by resetting the thread function pointer to EntryPoint1
*/
void Detach( void ){
m_pThreadFunc = CThread::EntryPoint;
}
protected:
/*
* Info: DONT override this method.
*
* This function is like a standard template.
* Override if you are sure of what you are doing.
*/
static DWORD WINAPI EntryPoint( LPVOID pArg)
{
CThread *pParent = reinterpret_cast<CThread*>(pArg);
pParent->ThreadCtor();
pParent->Run( pParent->m_ThreadCtx.m_pUserData );
pParent->ThreadDtor();
return STILL_ACTIVE;
}
/*
* Info: Override this method.
*
* This function should contain the body/code of your thread.
* Notice the signature is similar to that of any worker thread function
* except for the calling convention.
*/
virtual DWORD Run( LPVOID /* arg */ )
{ return m_ThreadCtx.m_dwExitCode; }
/*
* Info: Constructor-like function.
*
* Will be called by EntryPoint before executing the thread body.
* Override this function to provide your extra initialization.
*
* NOTE: do not confuse it with the classes constructor
*/
virtual void ThreadCtor(){ }
/*
* Info: Destructor-like function.
*
* Will be called by EntryPoint after executing the thread body.
* Override this function to provide your extra destruction.
*
* NOTE: do not confuse it with the classes constructor
*/
virtual void ThreadDtor(){ }
private:
/*
* Info: Thread Context Inner Class
*
* Every thread object needs to be associated with a set of values.
* like UserData Pointer, Handle, Thread ID etc.
*
* NOTE: This class can be enhanced to varying functionalities
* eg.,
* * Members to hold StackSize
* * SECURITY_ATTRIBUTES member.
*/
class CThreadContext
{
public:
CThreadContext(){
memset(this, 0, sizeof(this));
}
/*
* Attributes Section
*/
public:
HANDLE m_hThread; // The Thread Handle
DWORD m_dwTID; // The Thread ID
LPVOID m_pUserData; // The user data pointer
LPVOID m_pParent; // The this pointer of the parent CThread object
DWORD m_dwExitCode; // The Exit Code of the thread
};
/*
* Attributes Section
*/
protected:
/*
* Info: Members of CThread
*/
CThreadContext m_ThreadCtx; // The Thread Context member
LPTHREAD_START_ROUTINE m_pThreadFunc; // The Worker Thread Function Pointer
};
#endif //__THREAD_H__