-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathThreadPool.h
More file actions
105 lines (91 loc) · 2.18 KB
/
Copy pathThreadPool.h
File metadata and controls
105 lines (91 loc) · 2.18 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
#ifndef _THREADPOOL_H
#define _THREADPOOL_H
#include <vector>
#include <queue>
#include <thread>
#include <iostream>
#include <stdexcept>
#include <condition_variable>
#include <memory> //unique_ptr
const int MAX_THREADS = 100; //最大线程数目
template <typename T>
class threadPool {
public:
threadPool(int number =1 );
~threadPool();
bool append(T *request);
private:
static void * worker(void *arg);
void run();
private:
std::vector<std::thread> work_threads;
std::queue<T *> tasks_queue;
std::mutex queue_mutex;
std::condition_variable condition;
bool stop;
};
template <typename T>
threadPool<T>::threadPool(int number) : stop(false)
{
if (number <= 0 || number > MAX_THREADS)
throw std::exception();
for (int i = 0; i < number; i++)
{
std::cout << "创建第" << i << "个线程 " << std::endl;
/*
std::thread temp(worker, this);
不能先构造再插入
*/
work_threads.emplace_back(worker, this);//啥意思,初始化work,使用this 指针。
}
}
template <typename T>
void *threadPool<T>::worker(void *arg)//work是一个中间体,作为静态成员函数必须通过对象来访问成员函数
//这么做的意义是?如果不使用这个中间体会怎样?为啥是静态的
{
threadPool *pool = (threadPool *)arg; //thread_pool的this对象转换成pool指针
pool->run(); //用成员访问run。
return pool;
}
template <typename T>
void threadPool<T>::run()
{
while (!stop)
{
std::unique_lock<std::mutex> lk(this->queue_mutex);
this->condition.wait(lk, [this] { return !this->tasks_queue.empty(); });
if (this->tasks_queue.empty())
{
continue;
}
else
{
T *request = tasks_queue.front();
tasks_queue.pop();
if (request)
request->process();//这个 T*对象的precess是执行一次的意思吗?是传入要进行的动作
}
}
}
template <typename T>
inline threadPool<T>::~threadPool() {
{
std::unique_lock<std::mutex> lk(queue_mutex);
stop = true;
}
condition.notify_all();
for (auto& ii : work_threads)
{
ii.join();
}
}
template <typename T>
bool threadPool<T>::append(T *request) {
{
std::unique_lock<std::mutex> ul(queue_mutex);
tasks_queue.push(request);
}
condition.notify_one();
return true;
}
#endif