-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathEventLoopThreadPool.cpp
More file actions
35 lines (31 loc) · 976 Bytes
/
EventLoopThreadPool.cpp
File metadata and controls
35 lines (31 loc) · 976 Bytes
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
#include "EventLoopThreadPool.h"
#include "utils/Logging.h"
EventLoopThreadPool::EventLoopThreadPool(EventLoop *mainLoop, const int &numThreads): mainLoop_(mainLoop), started_(false), numThreads_(numThreads), next_(0), workThreads_(numThreads_), workLoops_(numThreads_){
if(numThreads_ <= 0){
LOG<<"number of thread <= 0,error exit";
abort();
}
}
void EventLoopThreadPool::start(){
mainLoop_->assertInLoopThread();
started_ = true;
for(int i = 0; i< numThreads_; ++i){
std::shared_ptr<EventLoopThread> t(new EventLoopThread());
workThreads_[i] = t;
// start LoopThread( return current loop )
workLoops_[i] = t->startLoop();
}
}
EventLoop* EventLoopThreadPool::getNextWorkLoop(){
mainLoop_->assertInLoopThread();
assert(started_);
EventLoop *loop = mainLoop_;
if(!workLoops_.empty()){
loop = workLoops_[next_];
next_ = (next_ + 1) % numThreads_;
}
return loop;
}
EventLoopThreadPool::~EventLoopThreadPool(){
LOG<<"EventLoopThreadPool exit!";
}