-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheventloopthread.cc
More file actions
55 lines (52 loc) · 1.56 KB
/
Copy patheventloopthread.cc
File metadata and controls
55 lines (52 loc) · 1.56 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
50
51
52
53
54
55
#include"eventloopthread.h"
#include"eventloop.h"
EventLoopThread::EventLoopThread(const ThreadInitCallback &cb,
const std::string &name)
: m_loop(nullptr), m_exiting(false),
m_thread(std::bind(&EventLoopThread::threadFunc, this), name),
m_mutex(), m_cond(), m_callback(cb)
{
}
EventLoopThread::~EventLoopThread()
{
m_exiting = true;
if(m_loop != nullptr)
{
m_loop->quit();
m_thread.join();
}
}
EventLoop * EventLoopThread::startLoop()
{
m_thread.start(); //启动底层新线程,执行回调函数,
EventLoop * loop = nullptr;
{/* 临界区m_loop */
std::unique_lock<std::mutex> lock(m_mutex);
while(m_loop == nullptr)
{
m_cond.wait(lock);
}
loop = m_loop;
}/* 临界区m_loop */
return loop;
}
/**
* @brief Thread对象实际执行的线程函数,在单独的子线程中执行
*/
void EventLoopThread::threadFunc()
{
EventLoop loop; //构造一个独立的eventloop, 和m_thread一对一, one loop per thread的证据
if(m_callback) //如果m_callback(即ThreadInitCallback)不为空则执行此函数
{
m_callback(&loop);
}
{/* 临界区m_loop */
std::unique_lock<std::mutex> lock(m_mutex);
m_loop = &loop;
m_cond.notify_one(); //通知主线程的startLoop(), loop已经在子线程创建好了。
}/* 临界区m_loop */
loop.loop(); //EventLoop loop => Poller.poll
/* 执行到此处说明loop已经结束 退出循环 */
std::unique_lock<std::mutex> lock(m_mutex);
m_loop = nullptr;
}