-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththreadpool.cpp
More file actions
194 lines (165 loc) · 4.62 KB
/
Copy paththreadpool.cpp
File metadata and controls
194 lines (165 loc) · 4.62 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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include "threadpool.h"
//MutexLock ThreadPool::lock;
pthread_mutex_t ThreadPool::lock=PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t ThreadPool::notify = PTHREAD_COND_INITIALIZER;
std::vector<pthread_t> ThreadPool::threads;
std::vector<ThreadPoolTask> ThreadPool::queue;
int ThreadPool::thread_count = 0;
int ThreadPool::queue_size = 0;
int ThreadPool::head = 0;
int ThreadPool::tail = 0;
int ThreadPool::count = 0;
int ThreadPool::shutdown = 0;
int ThreadPool::started = 0;
int ThreadPool::threadpool_create(int _thread_count, int _queue_size)
{
bool err = false;
do
{
if(_thread_count <= 0 || _thread_count > MAX_THREADS || _queue_size <= 0 || _queue_size > MAX_QUEUE)
{
_thread_count = 4;
_queue_size = 1024;
}
thread_count = 0;
queue_size = _queue_size;
head = tail = count = 0;
shutdown = started = 0;
threads.resize(_thread_count);
queue.resize(_queue_size);
/* Start worker threads */
for(int i = 0; i < _thread_count; ++i)
{
if(pthread_create(&threads[i], NULL, threadpool_thread, (void*)(0)) != 0)
{
//threadpool_destroy(pool, 0);
return -1;
}
++thread_count;
++started;
}
} while(false);
if (err)
{
//threadpool_free(pool);
return -1;
}
return 0;
}
void myHandler(std::shared_ptr<void> req)
{
std::shared_ptr<RequestData> request = std::static_pointer_cast<RequestData>(req);
request->handleRequest();
}
int ThreadPool::threadpool_add(std::shared_ptr<void> args, std::function<void(std::shared_ptr<void>)> fun)
{
int next, err = 0;
if(pthread_mutex_lock(&lock) != 0)
return THREADPOOL_LOCK_FAILURE;
//MutexLockGuard locker(lock);
do
{
next = (tail + 1) % queue_size;
// 队列满
if(count == queue_size)
{
err = THREADPOOL_QUEUE_FULL;
break;
}
// 已关闭
if(shutdown)
{
err = THREADPOOL_SHUTDOWN;
break;
}
queue[tail].fun = fun;
queue[tail].args = args;
tail = next;
++count;
/* pthread_cond_broadcast */
if(pthread_cond_signal(¬ify) != 0)
{
err = THREADPOOL_LOCK_FAILURE;
break;
}
} while(false);
if(pthread_mutex_unlock(&lock) != 0)
err = THREADPOOL_LOCK_FAILURE;
return err;
}
int ThreadPool::threadpool_destroy(ShutDownOption shutdown_option)
{
printf("Thread pool destroy !\n");
int i, err = 0;
if(pthread_mutex_lock(&lock) != 0)
{
return THREADPOOL_LOCK_FAILURE;
}
//MutexLockGuard locker(lock);
do
{
if(shutdown) {
err = THREADPOOL_SHUTDOWN;
break;
}
shutdown = shutdown_option;
if(pthread_cond_broadcast(¬ify) != 0)
{
err = THREADPOOL_LOCK_FAILURE;
break;
}
for(i = 0; i < thread_count; ++i)
{
if(pthread_join(threads[i], NULL) != 0)
{
err = THREADPOOL_THREAD_FAILURE;
}
}
} while(false);
if(!err)
{
threadpool_free();
}
return err;
}
int ThreadPool::threadpool_free()
{
if(started > 0)
return -1;
// pthread_mutex_lock(&lock);
// pthread_mutex_destroy(&lock);
pthread_cond_destroy(¬ify);
return 0;
}
void *ThreadPool::threadpool_thread(void *args)
{
while (true)
{
ThreadPoolTask task;
pthread_mutex_lock(&lock);
//MutexLockGuard locker(lock);//使用RAII 锁,服务器响应变慢???
while((count == 0) && (!shutdown))
{
//pthread_cond_wait(¬ify, lock.getlock());
pthread_cond_wait(¬ify, &lock);
}
if((shutdown == immediate_shutdown) ||
((shutdown == graceful_shutdown) && (count == 0)))
{
break;
}
task.fun = queue[head].fun;
task.args = queue[head].args;
queue[head].fun = NULL;
queue[head].args.reset();
head = (head + 1) % queue_size;
--count;
pthread_mutex_unlock(&lock);
(task.fun)(task.args);
}
--started;
pthread_mutex_unlock(&lock);
printf("This threadpool thread finishs!\n");
pthread_exit(NULL);
return(NULL);
}