-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThreadPool.hpp
More file actions
50 lines (33 loc) · 1.3 KB
/
Copy pathEventLoopThreadPool.hpp
File metadata and controls
50 lines (33 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#pragma once
#include "noncopyable.hpp"
#include <functional>
#include <string>
#include <vector>
#include <memory>
class EventLoop;
class EventLoopThread;
class EventLoopThreadPool : private 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();
// 返回是否启动
bool started() const { return started_; }
// 返回名称
const std::string name() const { return name_; }
private:
EventLoop *baseLoop_; // 用户创建的 基础 EventLoop,相当于 MainLoop, 如果是单线程,就只有这一个
std::string name_;
bool started_;
int numThreads_;
int next_;
std::vector<std::unique_ptr<EventLoopThread>> threads_; // 每个 EventLoopThread 资源的智能指针
// 调用EventLoopThread->startloop()会返回一个loop指针
std::vector<EventLoop *> loops_; // 记录每个线程中 EventLoop 的指针,
};