Repository navigation
Expand file tree
/
Copy pathghttp.cpp
More file actions
225 lines (207 loc) · 3.76 KB
/
Copy pathghttp.cpp
File metadata and controls
225 lines (207 loc) · 3.76 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
#define THREAD_NUM 30
class Task
{
public:
bool update;
string db_name;
string format;
string db_query;
const shared_ptr<HttpServer::Response> response;
const shared_ptr<HttpServer::Request> request;
Task(bool flag, string name, string ft, string query, const shared_ptr<HttpServer::Response>& res, const shared_ptr<HttpServer::Request>& req);
~Task();
void run();
};
Task::Task(bool flag, string name, string ft, string query, const shared_ptr<HttpServer::Response>& res, const shared_ptr<HttpServer::Request>& req):response(res),request(req)
{
update = flag;
db_name = name;
format = ft;
db_query = query;
}
Task::~Task()
{
}
void Task::run()
{
query_thread(update, db_name, format, db_query, response, request);
}
class Thread
{
public:
thread TD;
int ID;
static int threadnum;
Task* task;
Thread();
~Thread();
int GetThreadID();
void assign(Task* t);
void run();
void start();
friend bool operator==(Thread t1, Thread t2);
friend bool operator!=(Thread t1, Thread t2);
};
list<Thread*> busythreads;
vector<Thread*> freethreads;
mutex busy_mutex;
mutex free_mutex;
mutex task_mutex;
condition_variable task_cond;
void BackToFree(Thread *t)
{
busy_mutex.lock();
busythreads.erase(find(busythreads.begin(), busythreads.end(), t));
busy_mutex.unlock();
free_mutex.lock();
freethreads.push_back(t);
free_mutex.unlock();
}
int Thread::threadnum = 0;
Thread::Thread()
{
threadnum++;
ID = threadnum;
}
Thread::~Thread()
{
}
int Thread::GetThreadID()
{
return ID;
}
void Thread::assign(Task* t)
{
task = t;
}
void Thread::run()
{
cout << "Thread:" << ID << " run\n";
task->run();
delete task;
BackToFree(this);
}
void Thread::start()
{
TD = thread(&Thread::run, this);
TD.detach();
}
bool operator==(Thread t1, Thread t2)
{
return t1.ID == t2.ID;
}
bool operator!=(Thread t1, Thread t2)
{
return !(t1.ID == t2.ID);
}
class ThreadPool
{
public:
int ThreadNum;
bool isclose;
thread ThreadsManage;
queue<Task*> tasklines;
ThreadPool();
ThreadPool(int t);
~ThreadPool();
void create();
void SetThreadNum(int t);
int GetThreadNum();
void AddTask(Task* t);
void start();
void close();
};
ThreadPool::ThreadPool()
{
isclose = false;
ThreadNum = 10;
busythreads.clear();
freethreads.clear();
for (int i = 0; i < ThreadNum; i++)
{
Thread *p = new Thread();
freethreads.push_back(p);
}
}
ThreadPool::ThreadPool(int t)
{
isclose = false;
ThreadNum = t;
busythreads.clear();
freethreads.clear();
for (int i = 0; i < t; i++)
{
Thread *p = new Thread();
freethreads.push_back(p);
}
}
ThreadPool::~ThreadPool()
{
for (vector<Thread*>::iterator i = freethreads.begin(); i != freethreads.end(); i++)
delete *i;
}
void ThreadPool::create()
{
ThreadsManage = thread(&ThreadPool::start, this);
ThreadsManage.detach();
}
void ThreadPool::SetThreadNum(int t)
{
ThreadNum = t;
}
int ThreadPool::GetThreadNum()
{
return ThreadNum;
}
void ThreadPool::AddTask(Task* t)
{
unique_lock<mutex> locker(task_mutex);
tasklines.push(t);
locker.unlock();
task_cond.notify_one();
}
void ThreadPool::start()
{
while (true)
{
if (isclose == true)
{
busy_mutex.lock();
if (busythreads.size() != 0)
{
busy_mutex.unlock();
continue;
}
busy_mutex.unlock();
break;
}
free_mutex.lock();
if (freethreads.size() == 0)
{
free_mutex.unlock();
continue;
}
free_mutex.unlock();
unique_lock<mutex> locker(task_mutex);
while (tasklines.size() == 0)
task_cond.wait(locker);
Task *job = tasklines.front();
tasklines.pop();
locker.unlock();
free_mutex.lock();
Thread *t = freethreads.back();
freethreads.pop_back();
t->assign(job);
free_mutex.unlock();
busy_mutex.lock();
busythreads.push_back(t);
busy_mutex.unlock();
t->start();
}
}
void ThreadPool::close()
{
isclose = true;
}
ThreadPool pool(THREAD_NUM);
pool.create();