当前位置:网站首页>[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;
}边栏推荐
- Source code of YY music wechat applet imitating Netease cloud music
- The VM session was closed before any attempt to power it on
- wordpress去掉网站发布时间
- ETL tool (data synchronization) II
- Serialize common default values and column parameters
- 762. Prime number calculation setting in binary representation
- @Import
- TS class
- [QNX Hypervisor 2.2用户手册]9.6 gdb
- 推荐系统——An Embedding Learning Framework for Numerical Features in CTR Prediction
猜你喜欢

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

Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK

Why are there many snapshot tables in the BI system?

Npm+ module loading mechanism

@Import

【JUC】并发需要了解的关键字volatile

学习探索-波浪
![[testing technology automated testing pytest] basic summary of pytest](/img/30/7c632cd6ad93c9294745dda7642f17.png)
[testing technology automated testing pytest] basic summary of pytest

Inheritance (the child constructor inherits the attributes in the parent constructor)
![[nodejs] nodejs create a simple server](/img/00/7ab5629bf67777da21b41f44665880.png)
[nodejs] nodejs create a simple server
随机推荐
WebMvcConfigurationSupport
Summary of kotlin common knowledge points
Practical skills of easyexcel
Very simple vsplayaudio online music player plug-in
initializer_list工具库学习
PyTorch的数据输入格式要求及转换
Computed and watch listening properties
Serialize data type
加拿大EE通道
WebMvcConfigurationSupport
Node基础
Family relationship calculator wechat applet source code
The difference between MySQL clustered index and non clustered index
Npm+ module loading mechanism
Serialize operator
动态内存管理
idea设置get、set模板解决boolean类型字段的命名问题
BI 系统中为什么会有很多快照表?
电商RPA,大促轻松上阵的法宝
[QNX Hypervisor 2.2用户手册]9.7 generate