| 10 | class EventLoopThread; |
| 11 | |
| 12 | class EventLoopThreadPool |
| 13 | { |
| 14 | public: |
| 15 | // 用户传入的函数 |
| 16 | using ThreadInitCallback = std::function<void(EventLoop*)>; |
| 17 | EventLoopThreadPool(EventLoop *baseLoop, const std::string &nameArg); |
| 18 | ~EventLoopThreadPool(); |
| 19 | |
| 20 | // 设置线程数量 |
| 21 | void setThreadNum(int numThreads) { numThreads_ = numThreads; } |
| 22 | |
| 23 | // 启动线程池 |
| 24 | void start(const ThreadInitCallback &cb = ThreadInitCallback()); |
| 25 | |
| 26 | // 如果工作在多线程中,baseLoop_(mainLoop)会默认以轮询的方式分配Channel给subLoop |
| 27 | EventLoop *getNextLoop(); |
| 28 | |
| 29 | std::vector<EventLoop *> getAllLoops(); |
| 30 | |
| 31 | bool started() const { return started_; } |
| 32 | const std::string name() const { return name_; } |
| 33 | private: |
| 34 | EventLoop *baseLoop_; // 用户使用muduo创建的loop 如果线程数为1 那直接使用用户创建的loop 否则创建多EventLoop |
| 35 | std::string name_; |
| 36 | bool started_; // 开启线程池标志 |
| 37 | int numThreads_; // 创建线程数量 |
| 38 | size_t next_; // 轮询的下标 |
| 39 | std::vector<std::unique_ptr<EventLoopThread>> threads_; // 保存所有的EventLoopThread容器 |
| 40 | std::vector<EventLoop *> loops_; // 保存创建的所有EventLoop |
| 41 | }; |
| 42 | #endif // EVENT_LOOP_THREAD_POOL_H |
nothing calls this directly
no outgoing calls
no test coverage detected