当前位置:网站首页>【MUDUO】EventLoopThreadPool
【MUDUO】EventLoopThreadPool
2022-07-25 23:31:00 【CID( ͡ _ ͡ °)】
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());
// If you work in multithreading , baseLoop_ It will be allocated by polling by default Channel to
EventLoop* getNextLoop();
std::vector<EventLoop*> getAllLoops();
bool started() const { return started_ ; }
const std::string name() const { return name_; }
private:
EventLoop* baseLoop_;
std::string name_;
bool started_;
int numThreads_;
int next_; // Poll subscripts
std::vector<std::unique_ptr<EventLoopThread>> threads_;
std::vector<EventLoop *> loops_;
};
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()
{
//EventLoopThread Inside is the object on the stack , So destructors don't need to make declarations
}
void EventLoopThreadPool::start(const threadInitCallback &cb )
{
started_ = true;
// If the user sets the number of threads , It's going to get into this
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));
// The underlying thread is created , Bind a new EventLoop, And back to loop Address
loops_.push_back(t->startLoop());
}
// The whole server has only one thread , running baseloop ,
if(numThreads_ == 0 && cb)
{
cb(baseLoop_);
}
}
// If you work in multithreading , baseLoop_ It will be allocated by polling by default Channel to
EventLoop* EventLoopThreadPool::getNextLoop()
{
EventLoop *loop = baseLoop_;
if(!loops_.empty())
{
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
{
return loops_;
}
}边栏推荐
- 【MUDUO】Thread封装
- [Database Foundation] summary of MySQL Foundation
- [QNX hypervisor 2.2 user manual]9.7 generate
- PHP binary array is sorted by a field in it
- About priority queues
- 多模态——Deep Multi-Modal Sets
- Constructors and prototypes
- 生成随机数random学习之uniform_int_distribution,uniform_real_distribution
- Multimodal deep multi modal sets
- Mongodb query and projection operators
猜你喜欢
![[Database Foundation] summary of MySQL Foundation](/img/89/e22c232b0183eaae35a9f45a40ff36.jpg)
[Database Foundation] summary of MySQL Foundation

POI特效 市场调研

生成随机数random学习之uniform_int_distribution,uniform_real_distribution
![[code case] blog page design (with complete source code)](/img/9e/0e7cab956515b9cc75a7567eb477d2.png)
[code case] blog page design (with complete source code)

The VM session was closed before any attempt to power it on

File contains vulnerability

WordPress removes the website publishing time

@Import

Npm+ module loading mechanism

Apple CMS V10 template /mxone Pro adaptive film and television website template
随机推荐
动态内存管理
[QNX Hypervisor 2.2用户手册]9.7 generate
@Autowired注解 required属性
Recursion of function (use recursion to find the factorial of 1-N) (use recursion to find Fibonacci sequence) (use recursion to traverse data)
@Import
WordPress controls the minimum and maximum number of words of article comments
serialization and deserialization
numeric学习之iota,accumulate
1913. Maximum product difference between two number pairs - no sorting required
Docker 安装 Redis-5.0.12(远程访问)
Same origin strategy and cross domain
Discuz atmosphere game style template / imitation lol hero League game DZ game template GBK
Query commodity cases (operate data with array addition method) / key points
@Autowired annotation required attribute
Data broker understanding
TS class
The difference between MySQL clustered index and non clustered index
Practical skills of easyexcel
【MUDUO】EventLoop事件循环
JS get the current date and time