-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththread_pool.cpp
168 lines (122 loc) · 3.16 KB
/
thread_pool.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
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include "thread_pool.h"
#include "memory_pool.h"
#include "hz_error.h"
#include <fstream>
extern allocator hz_alloc;
extern std::fstream log_file;
thread_pool::thread_pool(int count) {
if (count <= 0) {
hz_error("thread error", "thread num is zero", log_file);
}
thread_count = 0;
queue_size = 0;
started = 0;
shutdown = false;
threads = (pthread_t *) hz_alloc.mem_palloc(sizeof(pthread_t) * count);
head = (tasks *) hz_alloc.mem_palloc(sizeof(tasks));
head->func = NULL;
head->arg = NULL;
head->next = NULL;
lock = (pthread_mutex_t*)malloc(sizeof(pthread_mutex_t));
cond = (pthread_cond_t*)malloc(sizeof(pthread_cond_t));
pthread_mutex_init(lock, NULL);
pthread_cond_init(cond, NULL);
for (int i = 0; i < count; ++i)
{
if(pthread_create(&threads[i], NULL, worker, (void*)this) != 0)
{
hz_error("thread error", "thread cread failed", log_file);
}
thread_count++;
started++;
}
}
void thread_pool::add(void (*func)(void *), void *arg)
{
if(pthread_mutex_lock(lock) != 0)
{
hz_error("thread error", "lock failed", log_file);
return;
}
tasks* task = (tasks*) hz_alloc.mem_palloc(sizeof(tasks));
task->func = func;
task->arg = arg;
task->next = head->next;
head->next = task;
queue_size++;
pthread_cond_signal(cond);
pthread_mutex_unlock(lock);
return;
}
void thread_pool::free()
{
if(started > 0)
{
hz_error("thread erro", "the thread is still started", log_file);
return ;
}
if(threads)
hz_alloc.mem_free((void*) threads);
tasks* old;
while(head->next)
{
old = head->next;
head->next = head->next->next;
hz_alloc.mem_free((void*) old);
}
return ;
}
thread_pool::~thread_pool()
{
pthread_mutex_lock(lock);
shutdown = true;
pthread_cond_broadcast(cond);
pthread_mutex_unlock(lock);
for(int i = 0; i<thread_count; i++)
{
if(pthread_join(threads[i], NULL) != 0)
{
hz_error("thread error", "pthread_join failed", log_file);
}
}
pthread_mutex_destroy(lock);
pthread_cond_destroy(cond);
::free(lock);
::free(cond);
free();
}
void* thread_pool::worker(void *arg)
{
if(arg == NULL)
{
hz_error("thread error", "pthread_join failed", log_file);
return NULL;
}
thread_pool *pool = (thread_pool*) arg;
tasks* task;
while(1)
{
pthread_mutex_lock(pool->lock);
while((pool->queue_size == 0) && !(pool->shutdown))
{
pthread_cond_wait((pool->cond), (pool->lock));
}
if(pool->shutdown)
break;
task = pool->head->next;
if(task == NULL)
{
pthread_mutex_unlock((pool->lock));
continue;
}
pool->head->next = task->next;
pool->queue_size--;
pthread_mutex_unlock((pool->lock));
(*(task->func))(task->arg);
hz_alloc.mem_free((void*) task);
}
pool->started--;
pthread_mutex_unlock((pool->lock));
pthread_exit(NULL);
return NULL;
}