1 Star 0 Fork 1

taozier/roughserver

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
EventLoopThreadPool.cpp 1.53 KB
一键复制 编辑 原始数据 按行查看 历史
taozier 提交于 2020-06-06 04:26 . update
#include "EventLoopThreadPool.h"
#include "EventLoop.h"
#include "EventLoopThread.h"
#include <assert.h>
EventLoopThreadPool::EventLoopThreadPool(EventLoop *baseLoop, int numThreads)
: baseLoop_(baseLoop), numThreads_(numThreads), start_(false), next_(0)
{
if (numThreads <= 0)
{
// LOG
abort();
}
}
EventLoopThreadPool::~EventLoopThreadPool() {}
void EventLoopThreadPool::start()
{
assert(!start_);
baseLoop_->assertInLoopThread();
start_ = true;
for (int i = 0; i < numThreads_; ++i)
{
EventLoopThread *t = new EventLoopThread(); //创建 EventLoopThread 对象
threads_.push_back(std::unique_ptr<EventLoopThread>(t)); // threads_ 内存储所有 EventLoopThread 对象的智能指针,实现自动销毁
eventLoops_.push_back(t->startLoop()); //在 EventLoopThread 对象内创建每个线程,并各自运行线程函数
}
}
EventLoop *EventLoopThreadPool::getNextLoop()
{
assert(start_);
baseLoop_->assertInLoopThread();
EventLoop *eventLoop = baseLoop_;
if (!eventLoops_.empty())
{
eventLoop = eventLoops_[next_]; //指向下一个事件循环
next_ = (next_ + 1) % numThreads_;
}
return eventLoop;
}
std::vector<EventLoop *> EventLoopThreadPool::getAllLoops()
{
assert(start_);
baseLoop_->assertInLoopThread();
if (!eventLoops_.empty())
{
return eventLoops_;
}
else
{
return std::vector<EventLoop *>(1, baseLoop_); //若未空 返回主线程
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
C++
1
https://gitee.com/taozier/roughserver.git
git@gitee.com:taozier/roughserver.git
taozier
roughserver
roughserver
master

搜索帮助