-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy paththread_pool.cpp
More file actions
40 lines (31 loc) · 1.08 KB
/
thread_pool.cpp
File metadata and controls
40 lines (31 loc) · 1.08 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
#include "thread_pool.hpp"
void* thread_func(void* arg){
pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
proxy_server_t* server = static_cast<proxy_server_t*>(arg);
server->start_server_loop();
std::cout << "thread finished " << pthread_self() << "\n";
return NULL;
}
thread_pool_t::thread_pool_t(size_t num_threads): num_threads(num_threads), next(0),
thread_ids(new pthread_t[num_threads]), proxy_servers(new proxy_server_t[num_threads]) {
for (size_t i = 0; i < num_threads; ++i){
int res = pthread_create(thread_ids + i, NULL, thread_func, proxy_servers + i);
if (res != 0){
errno = res;
perror("init thread_poll");
abort();
}
}
}
thread_pool_t::~thread_pool_t(){
for (size_t i = 0; i < num_threads; ++i){
pthread_cancel(thread_ids[i]);
pthread_join(thread_ids[i], NULL);
}
delete [] thread_ids;
delete [] proxy_servers;
}
void thread_pool_t::add_new_connection(int fd){
proxy_servers[next].add_client_socket(fd);
next = (next + 1) % num_threads;
}