WebServer----EventLoopThreadPool

EventLoopThreadPool简介

顾名思义,该线程池对象用于管理EventLoopThread对象。其结构很简单,数据成员主要包含所创建和管理的EventLoopThread、Eventloop数组,其中EventLoopThread对象的生命周期由该线程池管理,因此持有他的shared_ptr指针。

EventLoop* baseLoop_;    //由主函数创建的mianloop
bool started_;           //标识该线程池中的各线程是否运行
int numThreads_;         //主函数传递,用于确定创建多少线程
int next_;               //RR算法的基准值
//所创建的EventLoopThread对象的数组以及其所管理的EventLoop;
std::vector<std::shared_ptr<EventLoopThread>> threads_;
std::vector<EventLoop*> loops_;

构造函数只初始化baseloop_、started、numThread_和next_值

EventLoopThreadPool::EventLoopThreadPool(EventLoop* baseLoop, int numThreads)
        :   baseLoop_(baseLoop),
            started_(false),
            numThreads_(numThreads),
            next_(0)
{
    if (numThreads_ <= 0)
    {
        LOG << "numThreads_ <= 0";
        abort();
    }
}

真正创建线程对象是在start成员函数中:

void EventLoopThreadPool::start()
{
    baseLoop_->assertInLoopThread();
    started_ = true;
    for (int i = 0; i < numThreads_; ++i)
    {
        std::shared_ptr<EventLoopThread> t(new EventLoopThread());
        threads_.push_back(t);
        loops_.push_back(t->startLoop());
    }
}

RR算法获取下一个loop:

EventLoop *EventLoopThreadPool::getNextLoop()
{
    baseLoop_->assertInLoopThread();    
    assert(started_);
    EventLoop *loop = baseLoop_;
    if (!loops_.empty())
    {
        loop = loops_[next_];
        next_ = (next_ + 1) % numThreads_;
    }
    return loop;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值