-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.cpp
More file actions
32 lines (27 loc) · 841 Bytes
/
ThreadPool.cpp
File metadata and controls
32 lines (27 loc) · 841 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
//
// Created by parallels on 2020/1/20.
//
#include <unistd.h>
#include "ThreadPool.h"
ThreadPool::ThreadPool(EventLoop *baseloop) : baseLoop_(baseloop), next_(0), mutex_(), cond_(mutex_) {}
ThreadPool::~ThreadPool() {}
void ThreadPool::creatThreadPool() {
for(int i = 0; i < MAX_THREAD; i++) {
std::shared_ptr<EventLoopThread> t(new EventLoopThread());
{
mutex_.lock();
loops_.push_back(t->startLoop());
mutex_.unlock();
}
}
printf("ThreadPool create success\n");
}
//获取下一个线程事件循环指针,每一个连接都使用下一个线程,线程负载均衡
EventLoop *ThreadPool::getNext() {
EventLoop *loop = baseLoop_;
if (!loops_.empty()) {
loop = loops_[next_];
next_ = (next_ + 1) % MAX_THREAD;
}
return loop;
}