-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThreadPool.cpp
More file actions
64 lines (52 loc) · 1.52 KB
/
Copy pathEventLoopThreadPool.cpp
File metadata and controls
64 lines (52 loc) · 1.52 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include"EventLoopThread.h"
#include "EventLoopThreadPool.h"
#include<memory>
EventLoopThreadPool::EventLoopThreadPool(EventLoop *baseloop, const std::string &name)
:baseLoop_(baseloop)
,name_(name)
{
}
EventLoopThreadPool::~EventLoopThreadPool(){
}
void EventLoopThreadPool::start(const ThreadInitCallback &cb){
started_ = true;
/**多线程模式 创建其他loop(work loop)*/
for (size_t i = 0; i < numThreads_; ++i)
{
/**用户设置了多个线程模式*/
std::string buf(name_.data()+std::to_string(i));
std::unique_ptr<EventLoopThread> t = std::make_unique<EventLoopThread>(cb,buf);
threads_.emplace_back(std::move(t));
/**底层返回一个loop 这个loop是栈区的 */
loops_.push_back(threads_.back()->startLoop());
}
//调用端未设置线程数量
if (numThreads_ == 0 && cb)
{
cb(baseLoop_);
}
}
EventLoop *EventLoopThreadPool::getNextLoop(){
/**当不是多线程模式,则主要使用baseloop工作*/
EventLoop* loop = baseLoop_;
if (!loops_.empty())
{
/* 通过轮询的方式取loop工作*/
loop = loops_[next_];
++next_;
if (static_cast<size_t>(next_) >= loops_.size())
{
/**重置*/
next_ = 0;
}
}
return loop;
}
std::vector<EventLoop *> EventLoopThreadPool::getAllLoop(){
if (loops_.empty())
{
return std::vector<EventLoop*>(1,baseLoop_);
}else{
return loops_;
}
}