当前位置:网站首页>【MUDUO】打包EventLoop和Thread
【MUDUO】打包EventLoop和Thread
2022-07-25 23:22:00 【CID( ͡ _ ͡°)】
EventLoopThread.h
#pragma once
#include "noncopyable.h"
#include "Thread.h"
#include <functional>
#include <mutex>
#include <condition_variable>
#include <string>
class EventLoop;
class EventLoopThread:noncopyable
{
public:
using ThreadInitCallback = std::function<void(EventLoop*)>;
EventLoopThread(const ThreadInitCallback& cb = ThreadInitCallback(),
const std::string &name = std::string()
);
~EventLoopThread();
//开启循环
EventLoop* startLoop();
private:
void threadFunc();
//事件循环
EventLoop *loop_;
//是否退出循环
bool exiting_;
//线程
Thread thread_;
//加锁
std::mutex mutex_;
//条件变量
std::condition_variable cond_;
//回调函数初始化
ThreadInitCallback callback_;
};
EventLoopThread.cc
#include "EventLoopThread.h"
#include "EventLoop.h"
EventLoopThread::EventLoopThread(const ThreadInitCallback& cb , const std::string &name)
:loop_(nullptr) //是空的才会去创建一个线程
,exiting_(false)
,thread_(std::bind(&EventLoopThread::threadFunc, this),name)
,mutex_()
,cond_()
,callback_(cb)
{
}
EventLoopThread::~EventLoopThread()
{
exiting_ = true;
if(loop_ == nullptr)
{
loop_->quit();
thread_.join();
}
}
//开启循环
EventLoop* EventLoopThread::startLoop()
{
thread_.start();// 启动底层的线程
EventLoop *loop = nullptr;
{
std::unique_lock<std::mutex> lock(mutex_);
//等待线程创建成功void EventLoopThread::threadFunc() 的cond_.notify_one();通知
while(loop_ == nullptr)
{
cond_.wait(lock); //等待通知
}
loop = loop_;
}
return loop;
}
//这个是线程创建,并且开始Poller中的poll
void EventLoopThread::threadFunc()
{
EventLoop loop; //创建一个独立的EventLoop,和上面的线程一一对应
if(callback_)
{
callback_(&loop);
}
{
std::unique_lock<std::mutex> lock(mutex_);
loop_ = &loop;
cond_.notify_one(); //通知创建线程完成
}
loop.loop(); //此处开启了事件循环,
//到这里来证明服务器要关闭这个线程了
std::unique_lock<std::mutex> lock(mutex_);
loop_ = nullptr;
}边栏推荐
- How does Navicat modify the language (Chinese or English)?
- XXE&XML-外部实体注入-利用和绕过
- Call Gaode map -- address is converted into longitude and latitude
- Understanding English (speech understanding)
- [QNX Hypervisor 2.2用户手册]9.6 gdb
- Learning exploration-3d rotation card
- WordPress removes the website publishing time
- JS regular expression matches IP address (IP address regular expression verification)
- serialization and deserialization
- Thinkphp6 temporarily close the layout
猜你喜欢

Learning exploration-3d rotation card

serialization and deserialization

5 ROS simulation modeling (3-rviz+gazebo+ control simulation robot)

数组中重复的数字

Secure code warrior learning record (III)
![Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []](/img/06/85a6ba450fc2637a4ac1cf6a630912.png)
Explain in detail the addition (+) operation in JS, basic data type addition, reference data type addition, and the underlying operation rules, [] + {}, {} + []

About using NPM command under the terminal, the installation error problem is solved (my own experience)

WordPress removes the website publishing time

Longitude and latitude and its transformation with coordinate system

Why are there many snapshot tables in the BI system?
随机推荐
TS function
ASP date function (what if the disk function is incorrect)
About priority queues
Solution of phpstudy service environment 80 port occupied by process system under Windows
Wrote a little webapi knowledge points from 0 to 1
What is a physical firewall? What's the effect?
Simulink学习笔记(三)——Simulink自动代码生成(二)「建议收藏」
XxE & XML external entity injection utilization and bypass
1913. 两个数对之间的最大乘积差-无需排序法
MES系统设备管理概述(下)
Scaffold installation
WordPress removes the website publishing time
Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)
@Import
Overview of MES system equipment management (Part 2)
Discuz magazine / news report template (jeavi_line) utf8 GBK / DZ template download
VisualBox启动虚拟机报错:The VM session was closed before any attempt to power it on.
Summary of common PHP functions
[QNX hypervisor 2.2 user manual]9.8 load
[wechat applet] page navigation