-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathEventLoopThreadPool.cpp
42 lines (38 loc) · 1.12 KB
/
EventLoopThreadPool.cpp
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
/*************************************************************************
> File Name: EventLoopThreadPool.cpp
> Author: zhangfeng
> Mail: [email protected]
> Created Time: Mon 07 Oct 2019 05:35:02 PM CST
> Target:
************************************************************************/
#include "EventLoopThreadPool.h"
EventLoopThreadPool::EventLoopThreadPool(EventLoop *baseloop, int numThreads) :
baseLoop_(baseloop),
started_(false),
numThreads_(numThreads),
next_(0)
{
if(numThreads_ <= 0) {
LOG << "numThreads_ <= 0";
abort();
}
}
void EventLoopThreadPool::start() {
baseLoop_->assertInLoopThread();
started_ = true;
for(int i = 0; i < numThreads_; i++) {
std::shared_ptr<EventLoopThread> t(new EventLoopThread());
threads_.push_back(t);
loops_.push_back(t->startLoop());
}
}
EventLoop* EventLoopThreadPool::getNextLoop() {
baseLoop_->assertInLoopThread();
assert(started_);
EventLoop *loop = baseLoop_;
if(!loops_.empty()) {
loop = loops_[next_];
next_ = (next_ + 1) % numThreads_;
}
return loop;
}