-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThread.cpp
59 lines (46 loc) · 1.07 KB
/
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
#include "StdAfx.h"
#include "Thread.h"
#include "MessageLoop.h"
Thread::Thread(void)
: thread_(NULL)
, loop_(NULL) {
init_event_ = ::CreateEvent(NULL, TRUE, FALSE, NULL);
}
Thread::~Thread(void) {
Shutdown();
CloseHandle(init_event_);
}
DWORD __stdcall WorkThreadMain(LPVOID lpThreadParameter) {
Thread* thread = static_cast<Thread*>(lpThreadParameter);
if (thread) {
thread->Run();
}
return 0;
}
void Thread::Start() {
if (thread_) {
return ;
}
thread_ = ::CreateThread(NULL, 0, WorkThreadMain, this, NULL, NULL);
::ResetEvent(init_event_);
::WaitForSingleObject(init_event_, INFINITE);
}
void Thread::Shutdown() {
LOG(INFO) << __FUNCTION__ " | Enter";
if (loop_) {
loop_->Quit();
}
if (thread_) {
::WaitForSingleObject(thread_, INFINITE);
::CloseHandle(thread_);
thread_ = NULL;
}
LOG(INFO) << __FUNCTION__ " | Exit";
}
void Thread::Run() {
loop_ = new MessageLoopBase();
::SetEvent(init_event_);
loop_->RunLoop();
delete loop_;
loop_ = NULL;
}