muduo之EventLoopThreadPool
EventLoopThreadPool是IO线程对应的线程池,通过调用start函数来new EventLoopThread创建对应的线程和其loop,并将创建的保存在vector中。创建TcpConnection时,就会从EventLoopThreadPool中获取一个IO线程。
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();void setThreadNum(int numThreads) { numThreads_ = numThreads; }void start(const ThreadInitCallback& cb = ThreadInitCallback());// valid after calling start()/// round-robinEventLoop* getNextLoop();/// with the same hash code, it will always return the same EventLoopEventLoop* getLoopForHash(size_t hashCode);std::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),name_(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相关推荐
- muduo之TcpServer
TcpServer拥有Acceptor类,新连接到达时new TcpConnection后续客户端和TcpConnection类交互.TcpServer管理连接和启动线程池,用Acceptor接受连接 ...
- muduo网络库之Acceptor、TcpServer
本篇博客针对Acceptor类和TcpServer类做下小结. 博客代码来自于陈硕的muduo网络库,github地址https://github.com/chenshuo/muduo 学习笔记: A ...
- 34muduo_net库源码分析(十)
1.连接关闭时序图 2.代码 1.TcpConnection.h // Copyright 2010, Shuo Chen. All rights reserved. // http://code.g ...
- 33muduo_net库源码分析(九)
1.TcpServer/TcpConnection (1)Acceptor类的主要功能是socket.bind.listen (2)一般来说,在上层应用程序中,我们不直接使用Acceptor,而是把它 ...
- muduo网络库学习(八)事件驱动循环线程池EventLoopThreadPool
muduo是支持多线程的网络库,在muduo网络库学习(七)用于创建服务器的类TcpServer中也提及了TcpServer中有一个事件驱动循环线程池,线程池中存在大量线程,每个线程运行一个Event ...
- muduo源码client/server通信流程
今天来学习一下muduo源码中client和server间的大致通信流程,以echo服务为例,先看一下echo对面的main函数代码. #include "examples/simple/e ...
- muduo网络库学习(七)用于创建服务器的类TcpServer
目前为止,涉及到的绝大多数操作都没有提及线程,EventLoop,Poller,Channel,Acceptor,TcpConnection,这些对象的执行都是在单独线程完成,并没有设计多线程的创建销 ...
- muduo网络库学习(四)事件驱动循环EventLoop
muduo的设计采用高并发服务器框架中的one loop per thread模式,即一个线程一个事件循环. 这里的loop,其实就是muduo中的EventLoop,所以到目前为止,不管是Polle ...
- 基于C++11的muduo网络库
文章目录 写在前面 项目编译问题 库安装的问题 项目测试代码 关于压力测试 项目概述 muduo网络库的reactor模型 muduo的设计 muduo各个类 辅助类 NonCopyable Time ...
最新文章
- 计算机音乐谱打上花火,原神乐谱打上花火
- HALCON从像素坐标得到世界坐标
- android .9图制作
- C#--动态操作DataTable
- RESTful API 中的 Status code 是否要遵守规范
- python自动化办公:邮件篇 (定时邮件问候女票so easy)
- Java集合—Deque Stack
- RGB与YUV相互转换
- 我用Python玩小游戏“跳一跳”,瞬间称霸了朋友圈!
- 普通程序员转型深度学习指南
- Kubernetes1.91(K8s)安装部署过程(一)--证书安装
- 每个程序员都必须搞懂的抽象类和接口的含义以及区别
- 新人开车——访问控制
- mysql odbc 没有_如何解决mysql odbc安装丢失的问题
- python文件操作
- 如何绘制景区热力图_百度地图“景区热力图” 大数据让你拥有“千里眼”
- Go语言学习 二十一 内嵌
- 王者荣耀那么火 但你知道创始人背后的故事吗
- git merge 合并时 --no-ff 的作用——主要影响版本回退(好文章!)
- 决策引擎平台建设方案
热门文章
- 【Android FFMPEG 开发】FFMPEG ANativeWindow 原生绘制 ( 设置 ANativeWindow 缓冲区属性 | 获取绘制缓冲区 | 填充数据到缓冲区 | 启动绘制 )
- python中为什么需要使用“if __name__ == '__main__'”语句
- mybatis无mapper.xml用法
- mysql 锁-比较详细、深入的介绍
- Excel教程(12) - 数学和三角函数
- c# java数据类型不同点
- 快速清空mysql表的方法
- django BUG!!! === Django model coercing to Unicode: need string or buffer, XXX found
- 键盘I/O中断调用(INT 16H)和常见的int 17H、int 1A H
- 检测子进程的结束返回状态,status的取值可以是哪些?(简析)