-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patheventLoopThread.cpp
More file actions
49 lines (46 loc) · 1.36 KB
/
Copy patheventLoopThread.cpp
File metadata and controls
49 lines (46 loc) · 1.36 KB
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
#include "eventLoopThread.hpp"
EventLoopThread::EventLoopThread(const threadInitCallBack &cb, const string& name)
: _loop(nullptr)
, _exiting(false)
, _thread(bind(&EventLoopThread::threadFunc, this), name)
, _mutex()
, _cond()
, _callback(cb)
{
}
EventLoopThread::~EventLoopThread(){
_exiting = false;
if(_loop != nullptr){
_loop->quit();
_thread.join(); //主线程等待它创立的子线程结束运行
}
}
//建立新线程,并在新线程里创建loop并开始循环,返回新建立的loop
EventLoop* EventLoopThread::startLoop(){
_thread.start(); //子线程去运行了
//主线程继续往下走
EventLoop* loop = nullptr;
{
unique_lock<mutex> lock(_mutex);
while(_loop == nullptr){
_cond.wait(lock); //使用条件变量来保证子线程运行到了给_loop赋值那一步
}
loop = _loop;
}
return loop;
}
//启动一个新线程不是要传入一个线程函数嘛,就传它。这是子线程去建立eventLoop的过程
void EventLoopThread::threadFunc(){
EventLoop loop; //建立一个新的线程
if(_callback){
_callback(&loop);
}
{
unique_lock<mutex> lock(_mutex);
_loop = &loop;
_cond.notify_one();
}
loop.loop();
unique_lock<std::mutex> lock(_mutex); //子线程的loop退出循环了
_loop = nullptr;
}