muduo 20 EventLoopThreadPool

目录

成员变量:

成员函数:

EventLoopThreadPool() ~EventLoopThreadPool() 

EventLoopThreadPool::start

EventLoopThreadPool::getNextLoop()

EventLoopThreadPool.h

EventLoopThreadPool.cc


成员变量:

事件线程池的概念,两个主要容器:

 一个是所有线程 、 一个是EventLoopThread中的EventLoop指针。

std::vector<std::unique_ptr<EventLoopThread>> threads_;//所有事件的线程 
std::vector<EventLoop*> loops_;//事件线程EventLoopThread里面的EventLoop指针 

 有一个最基本的loop,也是mainloop:

EventLoop *baseLoop_;

 记录名称、开始标识、线程数量、做轮询操作的下标:

std::string name_;
bool started_;
int numThreads_;  //线程数量
int next_;//做轮询的下标使用的 

成员函数:

EventLoopThreadPool() ~EventLoopThreadPool() 


 

EventLoopThreadPool::start

 这里传入的cb回调函数会在EvenLoopthread中执行。

①:循环创建线程;

②&③:创建所有所有线程 、EventLoopThread中的EventLoop指针。

threads_.push_back(std::unique_ptr<EventLoopThread>(t));//unique_ptr,不想手动delete
loops_.push_back(t->startLoop());//底层创建线程,绑定一个新的EventLoop,并返回该loop的地址

        该函数创建指定数量的EventLoopThread对象并把其加入到容器中,底层创建线程,绑定一个新的EventLoop,并返回该loop的地址。

④:如果是只有一个线程,运行baseloop,就是用户创建的mainloop


EventLoopThreadPool::getNextLoop()

        我们创建的baseLoop_相当于是一个mainloop,他只是用来处理新用户的连接,还需要工作线程去做连接用户的读写事件,也就是说要baseLoop_默认以轮询的方式分配channel给subloop。

EventLoopThreadPool.h

#pragma once
#include "noncopyable.h"

#include <functional>
#include <string>
#include <vector>
#include <memory>

class EventLoop;
class EventLoopThread;

class EventLoopThreadPool : noncopyable
{
public:
    using ThreadInitCallback = std::function<void(EventLoop*)>; 

    EventLoopThreadPool(EventLoop *baseLoop, const std::string &nameArg);
    ~EventLoopThreadPool();

    void setThreadNum(int numThreads) { numThreads_ = numThreads; }//设置底层线程的数量 

    void start(const ThreadInitCallback &cb = ThreadInitCallback());//开启整个事件循环线程 

    //如果工作在多线程中,baseLoop_默认以轮询的方式分配channel给subloop
    EventLoop* getNextLoop();

    std::vector<EventLoop*> getAllLoops();//返回池里的所有loop 
 
    bool started() const { return started_; }
    const std::string name() const { return name_; }
private:

    EventLoop *baseLoop_;//最基本的loop,
	//对应一个线程,就是当前用户使用线程 EventLoop loop;负责用户的连接,已连接用户的读写 
	
    std::string name_;
    bool started_;
    int numThreads_;  //线程数量
    int next_;//做轮询的下标使用的 
    std::vector<std::unique_ptr<EventLoopThread>> threads_;//所有事件的线程 
    std::vector<EventLoop*> loops_;//事件线程EventLoopThread里面的EventLoop指针 
};

EventLoopThreadPool.cc

#include "EventLoopThreadPool.h"
#include "EventLoopThread.h"

#include <memory>

EventLoopThreadPool::EventLoopThreadPool(EventLoop *baseLoop, const std::string &nameArg)
    : baseLoop_(baseLoop)
    , name_(nameArg)
    , started_(false)
    , numThreads_(0)
    , next_(0)
{}

EventLoopThreadPool::~EventLoopThreadPool()
{}

void EventLoopThreadPool::start(const ThreadInitCallback &cb)
{
    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));//unique_ptr,不想手动delete
        loops_.push_back(t->startLoop());//底层创建线程,绑定一个新的EventLoop,并返回该loop的地址
    }

    //整个服务端只有一个线程,运行着baseloop,就是用户创建的mainloop
    if (numThreads_ == 0 && cb)
    {
        cb(baseLoop_);
    }
}

//如果工作在多线程中,baseLoop_默认以轮询的方式分配channel给subloop
EventLoop* EventLoopThreadPool::getNextLoop()
{
    EventLoop *loop = baseLoop_;//用户创建的mainloop

    if (!loops_.empty())//通过轮询获取下一个处理事件的loop
    {
        loop = loops_[next_];
        ++next_;
        if (next_ >= loops_.size())
        {
            next_ = 0;
        }
    }

    return loop;
}

std::vector<EventLoop*> EventLoopThreadPool::getAllLoops()
{
    if (loops_.empty())
    {
        return std::vector<EventLoop*>(1, baseLoop_);
    }
    else
    {
        loops_;
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值