@


前言

前面文章介绍了muduo网络库的单线程设计方式,即一个EventLoop 处理所有的事件,包括链接的建立、IO、计算、以及链接的销毁,本文介绍下muduo中的多线程设计方法。

多线程TcpServer

EventLoopThreadPool

多线程的muduo::TcpServer,主要通过添加一个EventLoopThreadPool 事件循环线程池实现,新建TcpConnection时从event loop pool里挑选一个loop给TcpConnection用。 也就是说多线程TcpServer自己的EventLoop只用来接受新连接, 而新连接会用其他EventLoop来执行IO。 (单线程TcpServer的EventLoop是与TcpConnection共享的。)

EventLoopThreadPooll 按one loop per thread的思想实现多线程TcpServer, 此时主线程循环只负责TCP链接的建立,及任务的分配,需要让哪个线程干活, 就把timer或IO(如TCP连接) 注册到那个线程的循环里即可;对实时性有要求的connection可以单独用一个线程; 数据量大的connection可以独占一个线程;并把数据处理任务分摊到另几个计算线程中(用线程池);其他次要的辅助性connections共享一个线程。

线程池设计模式

池是一种设计模式,线程是是一种资源,线程池设计模式通常是事先申请一定的资源,当需要使用时,去资源池中申请资源,用完后再放回资源池中。EventLoopThreadPool 正是借鉴了这种设计模式,虽然使用的线程并不会放回资源池中,但是本身的一个EventLoop即一个Reactor,本身就具备等待机制了。

muduo中的使用

TcpServer每次新建一条TcpConnection就会通过EventLoopThreadPool::getNextLoop()方法来取一个EventLoop, 目前的getNextLoop()只是循环的从池中取一条loop,如果提供给每条TcpConncetion的是均等的服务,那么这样就能很均匀的分配系统的资源了。

TcpServer的工作方式取决于EventLoopThreadPool中线程的创建数量。

0 意味着所有的I/O 都在TcpServer的主事件循环中,不会创建新的线程。

1 意味着所有的 I/O 在另一个线程中 ,TcpServer的主线程只负责建立连接。

N 意味着新的连接会被循环的分配到N条线程中工作。

连接的建立、消息、销毁

on_connection

1、对于新的连接new connection 会在 TcpServer的Loop 中由Acceptor建立,

void Acceptor::handleRead()
{
LOG_TRACE << "Acceptor::handleRead()";
p_loop->assertInLoopThread();
InetAddress peerAddr;
int connfd = m_acceptSocket.accept(&peerAddr);

2、之后从loop_thread_pool中取一个loop将新的connection注册上去。

void TcpServer::newConnetion(int sockfd, const InetAddress& peerAddr)
{
InetAddress localAddr(sockets::getLocalAddr(sockfd));
EventLoop* loop;
loop = ex_event_loop_thread_pool->getNextLoop();
TcpConnectionPtr conn(new TcpConnection(loop,
connName, sockfd, localAddr, peerAddr));
conn->setConnectionCallBack(m_connectionCallBack);
conn->setMessageCallBack(m_messageCallBack);
conn->setCloseCallBack(std::bind(&TcpServer::removeConnection, this, std::placeholders::_1));
loop->runInLoop(std::bind(&TcpConnection::connectEstablished, conn));

on_message

1、message将在connection自己所处的loop中处理,无需多言。

on_close

1、连接的销毁稍微复杂一点,connection会在自己的loop中调用Tcp:Server::removeConnection,我们需要将他移动到TcpServer的Loop线程中先解除TcpServer对connection的使用,然后在回到connection自己的loop中销毁连接connectDestroyed()。

void TcpServer::removeConnection(const TcpConnectionPtr& conn)
{
// FIXME: unsafe
p_loop->runInLoop(std::bind(&TcpServer::removeConnectionInLoop, this, conn));
} void TcpServer::removeConnectionInLoop(const TcpConnectionPtr& conn)
{
p_loop->assertInLoopThread();
LOG_INFO << "TcpServer::removeConnectionInLoop [" << m_name
<< "] - connection " << conn->name();
m_closeCallBack(conn);
EventLoop* ioLoop = conn->getLoop();
ioLoop->queueInLoop(
std::bind(&TcpConnection::connectDestroyed, conn));
}

简单透传服务实现

利用muduo多线程TcpServer 实现一个简单的透传服务,从connection中收到的消息直接转发给所有的其他connection。

//ws_tcp_server.hpp
#pragma once
#include <async_logging>
#include <muduo_server> namespace ws { extern std::unique_ptr<muduo::EventLoop> active_event_loop; class tcp_server{
public:
//! ctor
tcp_server(void);
//! dtor
~tcp_server(void) = default; //! copy ctor
tcp_server(const tcp_server&) = delete;
//! assignment operator
tcp_server& operator=(const tcp_server&) = delete; public: //! listen_and_serve
//! start server and listened on port
void listen_and_serve(std::size_t port); private: void on_connection(const muduo::TcpConnectionPtr&); void on_message(const muduo::TcpConnectionPtr& , muduo::Buffer*, ssize_t ); void on_close(const muduo::TcpConnectionPtr&);
private: //!
//typedef std::vector<muduo::TcpConnectionPtr> connections_t; //!
typedef std::map<std::string, muduo::TcpConnectionPtr> connections_map_t; private: //! master connection
//muduo::TcpConnectionPtr m_master_connection; //! slave connection
//connections_t m_connections; //! muduo tcp server
std::unique_ptr<muduo::TcpServer> m_tcp_server; //! connections map
connections_map_t m_connections_map; std::size_t cnt_connetions;
}; }
//ws_tcp_server.cpp
#include "ws_tcp_server.hpp" #include <assert.h> static muduo::EventLoopThread static_event_loop;
std::unique_ptr<muduo::EventLoop> ws::active_event_loop = std::unique_ptr<muduo::EventLoop>(static_event_loop.startLoop()); using namespace ws; tcp_server::tcp_server()
:cnt_connetions(0)
{ } void tcp_server::listen_and_serve(std::size_t port)
{
assert(active_event_loop); muduo::ex_event_loop_thread_pool = std::unique_ptr<muduo::EventLoopThreadPool>(new muduo::EventLoopThreadPool(ws::active_event_loop.get(), "event_loop", 3));
active_event_loop->runInLoop(std::bind(&muduo::EventLoopThreadPool::start, muduo::ex_event_loop_thread_pool.get())); m_tcp_server = std::unique_ptr<muduo::TcpServer>(new muduo::TcpServer(active_event_loop.get(), InetAddress(port))); m_tcp_server->setConnectionCallBack(std::bind(&tcp_server::on_connection, this, std::placeholders::_1));
m_tcp_server->setMessageCallBack(std::bind(&tcp_server::on_message, this, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
m_tcp_server->setCloseCallBack(std::bind(&tcp_server::on_close, this, std::placeholders::_1));
m_tcp_server->start();
} void tcp_server::on_connection(const muduo::TcpConnectionPtr& conn)
{
cnt_connetions++;
std::string conn_name = "tcp connection " + std::to_string(cnt_connetions);
conn->setConnectionName(conn_name); m_connections_map[conn_name] = std::move(conn); } void tcp_server::on_message(const muduo::TcpConnectionPtr& conn, muduo::Buffer* buf, ssize_t size)
{
connections_map_t::iterator iter;
for(iter = m_connections_map.begin(); iter != m_connections_map.end(); iter++)
{
if(iter->second != conn){
LOG_DEBUG << "con " << conn->name() << " send to " << iter->second->name();
iter->second->send(buf->peek(), size);
}
} buf->retrieve(size); } void tcp_server::on_close(const muduo::TcpConnectionPtr& conn)
{
size_t n = m_connections_map.erase(conn->name());
(void)n;
assert(n == 1);
}
// main.cpp
#include <async_logging>
#include "ws_tcp_server.hpp" int main()
{
Logger::setLogLevel(Logger::DEBUG); ws::tcp_server tcp_server;
tcp_server.listen_and_serve(8008); getchar();
}
../bin/ws_server
2019-03-19 16:22:17.202112 [TRACE] [EventLoopThread.cpp:41] [startLoop] EventLoopThread::startLoop() wait()
2019-03-19 16:22:17.203199 [TRACE] [TimerQueue.cpp:20] [createTimerfd] createTimerfd() fd : 4
2019-03-19 16:22:17.203376 [TRACE] [Epoll.cpp:115] [updateChannel] fd= 4 events 3
2019-03-19 16:22:17.203549 [TRACE] [EventLoop.cpp:23] [createEventfd] createEventfd() fd : 5
2019-03-19 16:22:17.203711 [INFO ] [EventLoop.cpp:43] EventLoop Create 0x7F1E72371B30 in thread 44057
2019-03-19 16:22:17.203872 [TRACE] [Epoll.cpp:115] [updateChannel] fd= 5 events 3
2019-03-19 16:22:17.204188 [TRACE] [EventLoopThread.cpp:65] [threadFunc] EventLoopThread::threadFunc() notify()
2019-03-19 16:22:17.204332 [TRACE] [EventLoop.cpp:74] [loop] EventLoop 0x7F1E72371B30 start loopig
2019-03-19 16:22:17.204477 [TRACE] [Epoll.cpp:72] [poll] Epoll::poll() maxConcurrencySize 2
2019-03-19 16:22:17.204794 [TRACE] [EventLoopThread.cpp:46] [startLoop] EventLoopThread::startLoop() wakeup
2019-03-19 16:22:17.210324 [INFO ] [EventLoop.cpp:43] EventLoop Create 0x7F1E71B70B30 in thread 44058
2019-03-19 16:22:17.212173 [INFO ] [EventLoop.cpp:43] EventLoop Create 0x7F1E7136FB30 in thread 44059
2019-03-19 16:22:17.212725 [INFO ] [EventLoop.cpp:43] EventLoop Create 0x7F1E70B6EB30 in thread 44060
2019-03-19 16:22:22.399790 [INFO ] [TcpServer.cpp:52] TcpServer::newConnetion() [Serv ] - new connection [Serv #1] from 127.0.0.1:51400
2019-03-19 16:22:22.399833 [INFO ] [TcpServer.cpp:61] Loop 0x7F1E71B70B30
2019-03-19 16:22:22.399842 [DEBUG] [TcpConnection.cpp:27] [TcpConnection] TcpConnection::ctor[Serv #1] at 0x7F1E6C0046F0 fd=17
2019-03-19 16:22:24.901612 [INFO ] [TcpServer.cpp:52] TcpServer::newConnetion() [Serv ] - new connection [Serv #2] from 127.0.0.1:51402
2019-03-19 16:22:24.901625 [INFO ] [TcpServer.cpp:61] Loop 0x7F1E7136FB30
2019-03-19 16:22:24.901635 [DEBUG] [TcpConnection.cpp:27] [TcpConnection] TcpConnection::ctor[Serv #2] at 0x7F1E6C006A30 fd=18
2019-03-19 16:22:25.903035 [DEBUG] [ws_tcp_server.cpp:58] [on_message] con tcp connection 2 send to tcp connection 1
2019-03-19 16:22:32.499161 [INFO ] [TcpServer.cpp:52] TcpServer::newConnetion() [Serv ] - new connection [Serv #3] from 127.0.0.1:51404
2019-03-19 16:22:32.499174 [INFO ] [TcpServer.cpp:61] Loop 0x7F1E70B6EB30
2019-03-19 16:22:32.499183 [DEBUG] [TcpConnection.cpp:27] [TcpConnection] TcpConnection::ctor[Serv #3] at 0x7F1E6C008D80 fd=19
2019-03-19 16:22:33.499744 [DEBUG] [ws_tcp_server.cpp:58] [on_message] con tcp connection 3 send to tcp connection 1
2019-03-19 16:22:33.499771 [DEBUG] [ws_tcp_server.cpp:58] [on_message] con tcp connection 3 send to tcp connection 2
2019-03-19 16:22:35.986563 [DEBUG] [ws_tcp_server.cpp:58] [on_message] con tcp connection 2 send to tcp connection 1
2019-03-19 16:22:35.986670 [DEBUG] [ws_tcp_server.cpp:58] [on_message] con tcp connection 2 send to tcp connection 3
2019-03-19 16:22:36.982713 [DEBUG] [ws_tcp_server.cpp:58] [on_message] con tcp connection 2 send to tcp connection 1
2019-03-19 16:22:36.982759 [DEBUG] [ws_tcp_server.cpp:58] [on_message] con tcp connection 2 send to tcp connection 3
2019-03-19 16:22:38.244229 [INFO ] [TcpServer.cpp:90] TcpServer::removeConnectionInLoop [Serv ] - connection tcp connection 2
2019-03-19 16:22:38.244281 [DEBUG] [TcpConnection.cpp:36] [~TcpConnection] TcpConnection::dtor[tcp connection 2] at 0x7F1E6C006A30 fd=18 state=kDisConnected

muduo学习笔记(六) 多线程的TcpServer的更多相关文章

  1. muduo学习笔记(二)Reactor关键结构

    目录 muduo学习笔记(二)Reactor关键结构 Reactor简述 什么是Reactor Reactor模型的优缺点 poll简述 poll使用样例 muduo Reactor关键结构 Chan ...

  2. 孙鑫VC学习笔记:多线程编程

    孙鑫VC学习笔记:多线程编程 SkySeraph Dec 11st 2010  HQU Email:zgzhaobo@gmail.com    QQ:452728574 Latest Modified ...

  3. java之jvm学习笔记六-十二(实践写自己的安全管理器)(jar包的代码认证和签名) (实践对jar包的代码签名) (策略文件)(策略和保护域) (访问控制器) (访问控制器的栈校验机制) (jvm基本结构)

    java之jvm学习笔记六(实践写自己的安全管理器) 安全管理器SecurityManager里设计的内容实在是非常的庞大,它的核心方法就是checkPerssiom这个方法里又调用 AccessCo ...

  4. Learning ROS for Robotics Programming Second Edition学习笔记(六) indigo xtion pro live

    中文译著已经出版,详情请参考:http://blog.csdn.net/ZhangRelay/article/category/6506865 Learning ROS for Robotics Pr ...

  5. Typescript 学习笔记六:接口

    中文网:https://www.tslang.cn/ 官网:http://www.typescriptlang.org/ 目录: Typescript 学习笔记一:介绍.安装.编译 Typescrip ...

  6. Muduo学习笔记(一) 什么都不做的EventLoop

    Muduo学习笔记(一) 什么都不做的EventLoop EventLoop EventLoop的基本接口包括构造.析构.loop(). One Loop Per Thread 一个线程只有一个Eve ...

  7. python3.4学习笔记(六) 常用快捷键使用技巧,持续更新

    python3.4学习笔记(六) 常用快捷键使用技巧,持续更新 安装IDLE后鼠标右键点击*.py 文件,可以看到Edit with IDLE 选择这个可以直接打开编辑器.IDLE默认不能显示行号,使 ...

  8. Go语言学习笔记六: 循环语句

    Go语言学习笔记六: 循环语句 今天学了一个格式化代码的命令:gofmt -w chapter6.go for循环 for循环有3种形式: for init; condition; increment ...

  9. 【opencv学习笔记六】图像的ROI区域选择与复制

    图像的数据量还是比较大的,对整张图片进行处理会影响我们的处理效率,因此常常只对图像中我们需要的部分进行处理,也就是感兴趣区域ROI.今天我们来看一下如何设置图像的感兴趣区域ROI.以及对ROI区域图像 ...

随机推荐

  1. Swift可选项

  2. nodejs之koa-router与koa-body搭配使用

    简介 koa需要搭配中间件来做接口更方便,使用Koa-body & Koa-router 使用 koa2 创建接口,处理post请求 const koa=require("koa&q ...

  3. servlet 乱码解决方法

    一. servlet 发送的html 页面中文乱码 解决方法, 1.加入如下代码 response.setCharacterEncoding("UTF-8"); 2.在html页面 ...

  4. java内部类和异常类的概念

    1.内部类的外嵌类的成员变量在内部类中任然有效,内部类中的方法也可以调用外嵌类中的 方法,内部类中不可以声明类的变量和方法,外嵌的类体可以用内部类声明对象,作为外嵌类的成员.内部类仅供他的外嵌类使用. ...

  5. java web----MINA框架使用

    前期准备 1.下载 http://mina.apache.org/ 2.将依赖包添加到工程目录下(在工程目录下创建libs(directory目录)) 3.将 slf4j-api-1.7.26.jar ...

  6. cf1144E 假高精度平均数

    /* 先一轮求和,再一轮做除法 */ #include<bits/stdc++.h> using namespace std; ],s2[]; ],n; int main(){ cin&g ...

  7. DOBRI

    问题 : DOBRI 时间限制: 1 Sec  内存限制: 128 MB 题目描述 给出一个包含N个整数的序列A,定义这个序列A的前缀和数组为SUM数组 ,当SUM数组中的第i个元素等于在i前面的三个 ...

  8. this容易混淆的示例

    [注]this 永远不会混乱,混乱的是我们而已. /* this永远指向当前函数的主人. this混乱: 1.添加了定时器/延时器 2.事件绑定 [注]函数如果发生了赋值,this就混乱了. */ 示 ...

  9. 模块(import语句,from...import语句,_name_属性)

    1, 什么是模块? 模块就是一系列功能的集合体 模块分为四个通用的类别: 1), 使用python编写的.py文件(*****) 2), 已被编译为共享库或DLL的C或C++扩展 3), 把一系列模块 ...

  10. swagger2常用注解说明

    说明: 1.这里使用的版本:springfox-swagger2(2.4)springfox-swagger-ui (2.4) 2.这里是说明常用注解的含义和基本用法(也就是说已经对swagger进行 ...