forked from linyacool/WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThread.cpp
More file actions
executable file
·44 lines (38 loc) · 898 Bytes
/
EventLoopThread.cpp
File metadata and controls
executable file
·44 lines (38 loc) · 898 Bytes
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
// @Author Lin Ya
// @Email xxbbb@vip.qq.com
#include "EventLoopThread.h"
#include <functional>
EventLoopThread::EventLoopThread()
: loop_(NULL),
exiting_(false),
thread_(bind(&EventLoopThread::threadFunc, this), "EventLoopThread"),
mutex_(),
cond_(mutex_) {}
EventLoopThread::~EventLoopThread() {
exiting_ = true;
if (loop_ != NULL) {
loop_->quit();
thread_.join();
}
}
EventLoop* EventLoopThread::startLoop() {
assert(!thread_.started());
thread_.start();
{
MutexLockGuard lock(mutex_);
// 一直等到threadFun在Thread里真正跑起来
while (loop_ == NULL) cond_.wait();
}
return loop_;
}
void EventLoopThread::threadFunc() {
EventLoop loop;
{
MutexLockGuard lock(mutex_);
loop_ = &loop;
cond_.notify();
}
loop.loop();
// assert(exiting_);
loop_ = NULL;
}