-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.cpp
More file actions
74 lines (65 loc) · 1.68 KB
/
Copy pathThreadPool.cpp
File metadata and controls
74 lines (65 loc) · 1.68 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include "ThreadPool.h"
ThreadPool::ThreadPool() :
thread_count(0), queue_size(0),finished(false){
lock.unlock();
sem_init(&sem, 0, 0);
}
void ThreadPool::create(int thread_count, int queue_size)
{
if(thread_count <= 0 || thread_count > MAX_THREADS || queue_size < 0 || queue_size > MAX_QUEUE)
{
thread_count = MAX_THREADS;
queue_size = MAX_QUEUE;
}
this->thread_count = thread_count;
this->queue_size = queue_size;
threads.resize(thread_count);
for (int i = 0; i < thread_count; i++)
threads[i] = thread(work, this);
}
void ThreadPool::add(ThreadPoolTask* new_task)
{
if (queue.size() >= queue_size)
throw runtime_error("server busy");
lock.lock();
queue.push_back(new_task);
lock.unlock();
sem_post(&sem);
}
void ThreadPool::work(ThreadPool *thread_pool)
{
while (!thread_pool->finished)
{
ThreadPoolTask* task;
struct timespec ts;
clock_gettime(CLOCK_REALTIME, &ts);
ts.tv_sec += 1;
if (sem_timedwait(&thread_pool->sem, &ts) == 0){
thread_pool->lock.lock();
task = thread_pool->queue.front();
thread_pool->queue.pop_front();
thread_pool->lock.unlock();
try
{
task->run();
}
catch(const exception& e)
{
Log::log(e.what(), ERROR);
}
}
}
}
void ThreadPool::free(){
finished = true;
for (int i = 0; i < threads.size(); i++){
try
{
threads[i].join();
}
catch(const std::exception& e)
{
Log::log(e.what(), ERROR);
}
}
}