-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread.cpp
63 lines (56 loc) · 991 Bytes
/
thread.cpp
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
#include "thread.h"
sec::Thread::Thread(thread_run func)
{
this->func = func;
}
sec::Thread::~Thread()
{
if (this->alive)
{
#ifdef WIN32
if (handle_t)
CloseHandle(handle_t);
ExitThread(0);
//TerminateThread(handle_t, 0);
}
#endif // WIN32
this->func = 0;
}
void sec::Thread::start()
{
this->alive = true;
this->interrupt = false;
#ifdef WIN32
handle_t = CreateThread(NULL, 0, winThread, this, 0, &id);
if (!handle_t)
{
return;
}
//CloseHandle(handle_t);
#endif // WIN32
}
sec::thread_t sec::Thread::getId()
{
return this->id;
}
void sec::Thread::stop()
{
this->alive = true;
this->interrupt = true;
}
#ifdef WIN32
DWORD WINAPI winThread(LPVOID lpParamter)
{
sec::Thread *t = (sec::Thread*)lpParamter;
while (t->isAlive() && !t->isInterrupt())
{
t->run();
}
return NULL;
}
#endif // WIN32
void sec::Thread::run()
{
if (this->func)
func();
}