-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEventLoopThread.cpp
49 lines (44 loc) · 1.08 KB
/
EventLoopThread.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
/*************************************************************************
> File Name: EventLoopThread.cpp
> Author: zhangfeng
> Mail: [email protected]
> Created Time: Mon 07 Oct 2019 03:52:26 PM CST
> Target:
************************************************************************/
#include "EventLoopThread.h"
#include <functional>
EventLoopThread::EventLoopThread() :
loop_(nullptr),
existing_(false),
thread_(bind(&EventLoopThread::threadFunc, this), "EventLoopThread"),
mutex_(),
cond_(mutex_)
{}
EventLoopThread::~EventLoopThread() {
existing_ = true;
if(loop_ != nullptr) {
loop_->quit();
thread_.join();
}
}
EventLoop* EventLoopThread::startLoop() {
assert(!thread_.started());
thread_.start();
{
MutexLockGuard lock(mutex_);
while(loop_ == nullptr) {
cond_.wait();
}
}
return loop_;
}
void EventLoopThread::threadFunc() {
EventLoop loop;
{
MutexLockGuard lock(mutex_);
loop_ = &loop;
cond_.notify();
}
loop.loop();
loop_ = nullptr;
}