muduo源码剖析之EventLoopThreadPool

2023-10-25 01:52

本文主要是介绍muduo源码剖析之EventLoopThreadPool,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

简介

EventLoopThreadPool是EventLoopThread类的线程池类

封装了若干个EventLoopThread的线程池,所有者是一个外部的EventLoop

EventLoopThreadPool == EventLoopThread + vector

主要成员及属性解析

通过调用start函数来new EventLoopThread创建对应的线程和其loop,并将创建的保存在vector中

源码剖析

这个类比较简单,代码都写了注释,不多说

EventLoopThreadPool.h

// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)
//
// This is an internal header file, you should not include this.#ifndef MUDUO_NET_EVENTLOOPTHREADPOOL_H
#define MUDUO_NET_EVENTLOOPTHREADPOOL_H#include "muduo/base/noncopyable.h"
#include "muduo/base/Types.h"#include <functional>
#include <memory>
#include <vector>namespace muduo
{namespace net
{class EventLoop;
class EventLoopThread;class EventLoopThreadPool : noncopyable
{public:typedef std::function<void(EventLoop*)> ThreadInitCallback;EventLoopThreadPool(EventLoop* baseLoop, const string& nameArg);~EventLoopThreadPool();//设置subloop的数量void setThreadNum(int numThreads) { numThreads_ = numThreads; }//线程池启动,会创建设置好的EventLoopThread数量,并保存threads_中void start(const ThreadInitCallback& cb = ThreadInitCallback());// valid after calling start()/// round-robin//通过next变量保存下标,每调用一次就会获取下一个loopEventLoop* getNextLoop();/// with the same hash code, it will always return the same EventLoop//使用相同的哈希码,它将始终返回相同的事件循环,//通过hash码获取相应的loopEventLoop* getLoopForHash(size_t hashCode);//获取所有的subloopstd::vector<EventLoop*> getAllLoops();bool started() const{ return started_; }const string& name() const{ return name_; }private:EventLoop* baseLoop_;     //主线程的事件驱动循环,TcpServer所在的事件驱动循环,创建TcpServer传入的EventLoopstring name_;bool started_;//线程池启动标志int numThreads_;          //线程数 int next_;                //标记下次应该取出哪个线程,采用round_robinstd::vector<std::unique_ptr<EventLoopThread>> threads_;     //线程池中所有的线程 //线程池中每个线程对应的事件驱动循环,从线程池取出线程实际上返回的是事件驱动循环//每个事件驱动循环运行在一个线程中std::vector<EventLoop*> loops_;
};}  // namespace net
}  // namespace muduo#endif  // MUDUO_NET_EVENTLOOPTHREADPOOL_H

EventLoopThreadPool.cc

// Copyright 2010, Shuo Chen.  All rights reserved.
// http://code.google.com/p/muduo/
//
// Use of this source code is governed by a BSD-style license
// that can be found in the License file.// Author: Shuo Chen (chenshuo at chenshuo dot com)#include "muduo/net/EventLoopThreadPool.h"#include "muduo/net/EventLoop.h"
#include "muduo/net/EventLoopThread.h"#include <stdio.h>using namespace muduo;
using namespace muduo::net;//EventLoopThreadPool loop对应的线程池
EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop, const string& nameArg): baseLoop_(baseLoop),//设置mainloopname_(nameArg),started_(false),numThreads_(0),next_(0)
{
}EventLoopThreadPool::~EventLoopThreadPool()
{// Don't delete loop, it's stack variable
}void EventLoopThreadPool::start(const ThreadInitCallback& cb)   //开启线程池,创建线程
{assert(!started_);baseLoop_->assertInLoopThread();started_ = true;for (int i = 0; i < numThreads_; ++i){char buf[name_.size() + 32];snprintf(buf, sizeof buf, "%s%d", name_.c_str(), i);EventLoopThread* t = new EventLoopThread(cb, buf);threads_.push_back(std::unique_ptr<EventLoopThread>(t));loops_.push_back(t->startLoop());}if (numThreads_ == 0 && cb){cb(baseLoop_);}
}EventLoop* EventLoopThreadPool::getNextLoop()        //获取一个线程(事件驱动循环),通常在创建TcpConnection时调用 
{baseLoop_->assertInLoopThread();assert(started_);EventLoop* loop = baseLoop_;if (!loops_.empty()){// round-robinloop = loops_[next_];++next_;if (implicit_cast<size_t>(next_) >= loops_.size()){next_ = 0;}}return loop;
}EventLoop* EventLoopThreadPool::getLoopForHash(size_t hashCode)
{baseLoop_->assertInLoopThread();EventLoop* loop = baseLoop_;if (!loops_.empty()){loop = loops_[hashCode % loops_.size()];}return loop;
}std::vector<EventLoop*> EventLoopThreadPool::getAllLoops()
{baseLoop_->assertInLoopThread();assert(started_);if (loops_.empty()){return std::vector<EventLoop*>(1, baseLoop_);}else{return loops_;}
}

这篇关于muduo源码剖析之EventLoopThreadPool的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/279265

相关文章

8种快速易用的Python Matplotlib数据可视化方法汇总(附源码)

《8种快速易用的PythonMatplotlib数据可视化方法汇总(附源码)》你是否曾经面对一堆复杂的数据,却不知道如何让它们变得直观易懂?别慌,Python的Matplotlib库是你数据可视化的... 目录引言1. 折线图(Line Plot)——趋势分析2. 柱状图(Bar Chart)——对比分析3

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Java 正则表达式URL 匹配与源码全解析

《Java正则表达式URL匹配与源码全解析》在Web应用开发中,我们经常需要对URL进行格式验证,今天我们结合Java的Pattern和Matcher类,深入理解正则表达式在实际应用中... 目录1.正则表达式分解:2. 添加域名匹配 (2)3. 添加路径和查询参数匹配 (3) 4. 最终优化版本5.设计思

Java调用C++动态库超详细步骤讲解(附源码)

《Java调用C++动态库超详细步骤讲解(附源码)》C语言因其高效和接近硬件的特性,时常会被用在性能要求较高或者需要直接操作硬件的场合,:本文主要介绍Java调用C++动态库的相关资料,文中通过代... 目录一、直接调用C++库第一步:动态库生成(vs2017+qt5.12.10)第二步:Java调用C++

Python实现无痛修改第三方库源码的方法详解

《Python实现无痛修改第三方库源码的方法详解》很多时候,我们下载的第三方库是不会有需求不满足的情况,但也有极少的情况,第三方库没有兼顾到需求,本文将介绍几个修改源码的操作,大家可以根据需求进行选择... 目录需求不符合模拟示例 1. 修改源文件2. 继承修改3. 猴子补丁4. 追踪局部变量需求不符合很

Spring 中 BeanFactoryPostProcessor 的作用和示例源码分析

《Spring中BeanFactoryPostProcessor的作用和示例源码分析》Spring的BeanFactoryPostProcessor是容器初始化的扩展接口,允许在Bean实例化前... 目录一、概览1. 核心定位2. 核心功能详解3. 关键特性二、Spring 内置的 BeanFactory

Go中sync.Once源码的深度讲解

《Go中sync.Once源码的深度讲解》sync.Once是Go语言标准库中的一个同步原语,用于确保某个操作只执行一次,本文将从源码出发为大家详细介绍一下sync.Once的具体使用,x希望对大家有... 目录概念简单示例源码解读总结概念sync.Once是Go语言标准库中的一个同步原语,用于确保某个操

Java汇编源码如何查看环境搭建

《Java汇编源码如何查看环境搭建》:本文主要介绍如何在IntelliJIDEA开发环境中搭建字节码和汇编环境,以便更好地进行代码调优和JVM学习,首先,介绍了如何配置IntelliJIDEA以方... 目录一、简介二、在IDEA开发环境中搭建汇编环境2.1 在IDEA中搭建字节码查看环境2.1.1 搭建步

Node.js 中 http 模块的深度剖析与实战应用小结

《Node.js中http模块的深度剖析与实战应用小结》本文详细介绍了Node.js中的http模块,从创建HTTP服务器、处理请求与响应,到获取请求参数,每个环节都通过代码示例进行解析,旨在帮... 目录Node.js 中 http 模块的深度剖析与实战应用一、引言二、创建 HTTP 服务器:基石搭建(一