-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEventLoopThreadPool.cpp
More file actions
48 lines (41 loc) · 1.28 KB
/
Copy pathEventLoopThreadPool.cpp
File metadata and controls
48 lines (41 loc) · 1.28 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
#include "EventLoopThreadPool.h"
#include "EventLoopThread.h"
EventLoopThreadPool::EventLoopThreadPool(EventLoop* mainLoop, const std::string& name)
: mainLoop_(mainLoop),
name_(name),
started_(false),
numThreads_(0),
next_(0) {}
EventLoopThreadPool::~EventLoopThreadPool() {}
void EventLoopThreadPool::setThreadNum(int numThreads) {
numThreads_ = numThreads;
}
void EventLoopThreadPool::start(const ThreadInitCallback& cb) {
started_ = true;
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));
loops_.push_back(t->startLoop());
}
if (numThreads_ == 0 && cb) {
cb(mainLoop_);
}
}
EventLoop* EventLoopThreadPool::getNextLoop() {
EventLoop* loop = mainLoop_;
if (!loops_.empty()) {
// round-robin,此处可以优化
loop = loops_[next_];
next_ = (next_ + 1) % loops_.size();
}
return loop;
}
std::vector<EventLoop*> EventLoopThreadPool::getAllLoops() {
if (!loops_.empty()) {
return loops_;
} else {
return std::vector<EventLoop*> (1, mainLoop_);
}
}