当前位置:网站首页>[Muduo] package EventLoop and thread
[Muduo] package EventLoop and thread
2022-07-25 23:31: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();
// Turn on the cycle
EventLoop* startLoop();
private:
void threadFunc();
// The event loop
EventLoop *loop_;
// Whether to exit the cycle
bool exiting_;
// Threads
Thread thread_;
// Lock
std::mutex mutex_;
// Condition variables,
std::condition_variable cond_;
// Callback function initialization
ThreadInitCallback callback_;
};
EventLoopThread.cc
#include "EventLoopThread.h"
#include "EventLoop.h"
EventLoopThread::EventLoopThread(const ThreadInitCallback& cb , const std::string &name)
:loop_(nullptr) // Only when it is empty will it create a thread
,exiting_(false)
,thread_(std::bind(&EventLoopThread::threadFunc, this),name)
,mutex_()
,cond_()
,callback_(cb)
{
}
EventLoopThread::~EventLoopThread()
{
exiting_ = true;
if(loop_ == nullptr)
{
loop_->quit();
thread_.join();
}
}
// Turn on the cycle
EventLoop* EventLoopThread::startLoop()
{
thread_.start();// Start the underlying thread
EventLoop *loop = nullptr;
{
std::unique_lock<std::mutex> lock(mutex_);
// Wait for the thread to be created successfully void EventLoopThread::threadFunc() Of cond_.notify_one(); notice
while(loop_ == nullptr)
{
cond_.wait(lock); // Wait for a notice
}
loop = loop_;
}
return loop;
}
// This is thread creation , And started Poller Medium poll
void EventLoopThread::threadFunc()
{
EventLoop loop; // Create an independent EventLoop, Correspond to the above thread one by one
if(callback_)
{
callback_(&loop);
}
{
std::unique_lock<std::mutex> lock(mutex_);
loop_ = &loop;
cond_.notify_one(); // Notify the creation thread to complete
}
loop.loop(); // The event loop is opened here ,
// Come here to prove that the server is going to shut down this thread
std::unique_lock<std::mutex> lock(mutex_);
loop_ = nullptr;
}边栏推荐
- TS function
- 生成随机数random学习之uniform_int_distribution,uniform_real_distribution
- Thinkphp6 temporarily close the layout
- 152. 乘积最大子数组-动态规划
- Recommended system - an embedded learning framework for numerical features in CTR prediction
- E-commerce RPA, a magic weapon to promote easy entry
- PyTorch的数据输入格式要求及转换
- Duplicate numbers in array
- Serialize addition, deletion, modification and query
- Docker 安装 Redis-5.0.12(远程访问)
猜你喜欢

生成随机数random学习之uniform_int_distribution,uniform_real_distribution

Multimodal deep multi modal sets

Anti shake and throttling

@Import

idea设置get、set模板解决boolean类型字段的命名问题

Learning exploration-3d rotation card
![[code case] blog page design (with complete source code)](/img/9e/0e7cab956515b9cc75a7567eb477d2.png)
[code case] blog page design (with complete source code)

Take away applet with main version of traffic / repair to add main access function of traffic

Source code of wechat applet for discerning flowers and plants / source code of wechat applet for discerning plants

谷粒学苑P98踩坑 e.GlobalExceptionHandler : null
随机推荐
From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
About the foundation of fetch
Serialize operator
多模态——Deep Multi-Modal Sets
意向不到的Dubug妙招
utility实用组件学习之swap,move,forward,exchange
[nodejs] nodejs create a simple server
Learning exploration-3d rotation card
How to set pseudo static for WordPress fixed links
Which securities firm is the best and safest for beginners to open an account
chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted
类和对象(2)(6个默认成员函数)
网格参数化Least Squares Conformal Maps实现(3D网格映射到2D平面)
Data broker understanding
XXE&XML-外部实体注入-利用和绕过
numeric学习之iota,accumulate
【JUC】并发需要了解的关键字volatile
The difference between MySQL clustered index and non clustered index
This point inside the function / change this point inside the function
Cuteone: a onedrive multi network disk mounting program / with member / synchronization and other functions