-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThread.cc
More file actions
60 lines (51 loc) · 1.65 KB
/
EventLoopThread.cc
File metadata and controls
60 lines (51 loc) · 1.65 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
56
57
58
//
// Created by satellite on 2023-11-21.
//
#include "EventLoopThread.h"
#include "EventLoop.h"
#include <cassert>
EventLoopThread::EventLoopThread(EventLoopThread::ThreadInitCallback cb
, const std::string &name)
: loop_(nullptr)
, exiting_(false)
, thread_(std::bind(&EventLoopThread::threadFunc , this) , name)
, mutex_()
, cond_()
, callback_(std::move(cb))
{
}
EventLoopThread::~EventLoopThread() {
exiting_ = true;
if(loop_ != nullptr) {
loop_->quit();
thread_.join();
}
}
EventLoop *EventLoopThread::startLoop() {
assert(!thread_.started());
thread_.start(); //开启线程执行 threadFunc
EventLoop* loop = nullptr;
{
std::unique_lock<std::mutex> lock(mutex_);
while(loop_ != nullptr) {
cond_.wait(lock); //解锁等待notify
}
loop = loop_;
}
return loop;
}
void EventLoopThread::threadFunc() { //在单独的新线程内运行
EventLoop loop; //在新线程内创建一个EventLoop,one loop pre thread
if(callback_) {
callback_(&loop); //调用线程初始化回调函数
}
{
std::unique_lock<std::mutex> lock(mutex_);
loop_ = &loop;
cond_.notify_one();
}
loop.loop(); //开启Poller
//当可以执行到这里,说明poller::poll() 结束了,事件循环结束了
std::unique_lock<std::mutex> lock(mutex_);
loop_ = nullptr;
}