线程池
# 概念
线程池是一种多线程处理模型,它预先创建一组线程并管理它们的生命周期,用于执行大量短期或重复的任务。线程池的核心思想是复用线程,避免频繁创建和销毁线程带来的性能开销,从而提高程序的响应速度和系统资源利用率。
应用场景:
- 需要大量的线程来完成任务,且完成任务的时间比较短。
- 对性能要求苛刻的应用,比如要求服务器迅速响应客户请求。
- 接受突发性的大量请求,但不至于使服务器因此产生大量线程的应用。
#pragma once
#include <pthread.h>
#include <unistd.h>
#include <mutex>
#include <queue>
#include <vector>
#include "LockGuard.hpp"
#include "Thread.hpp"
using namespace ThreadNs;
const int gnum = 3;
template <class T>
class ThreadPool;
template <class T>
class ThreadData {
public:
ThreadPool<T>* threadpool;
std::string name;
public:
ThreadData(ThreadPool<T>* tp, const std::string& n)
: threadpool(tp), name(n) {}
};
template <class T>
class ThreadPool {
private:
static void* handlerTask(void* args) {
ThreadData<T>* td = (ThreadData<T>*)args;
while (true) {
T t;
{
LockGuard lockguard(td->threadpool->mutex());
while (td->threadpool->isQueueEmpty()) {
td->threadpool->threadWait();
}
t = td->threadpool->pop();
}
std::cout << td->name << " 获取了一个任务: " << t.toTaskString()
<< " 并处理完成,结果是:" << t() << std::endl;
}
delete td;
return nullptr;
}
ThreadPool(const int& num = gnum) : _num(num) {
pthread_mutex_init(&_mutex, nullptr);
pthread_cond_init(&_cond, nullptr);
for (int i = 0; i < _num; i++) {
_threads.push_back(new Thread());
}
}
void operator=(const ThreadPool&) = delete;
ThreadPool(const ThreadPool&) = delete;
public:
void lockQueue() { pthread_mutex_lock(&_mutex); }
void unlockQueue() { pthread_mutex_unlock(&_mutex); }
bool isQueueEmpty() { return _task_queue.empty(); }
void threadWait() { pthread_cond_wait(&_cond, &_mutex); }
T pop() {
T t = _task_queue.front();
_task_queue.pop();
return t;
}
pthread_mutex_t* mutex() { return &_mutex; }
public:
void run() {
for (const auto& t : _threads) {
ThreadData<T>* td = new ThreadData<T>(this, t->threadname());
t->start(handlerTask, td);
std::cout << t->threadname() << " start ..." << std::endl;
}
}
void push(const T& in) {
LockGuard lockguard(&_mutex);
_task_queue.push(in);
pthread_cond_signal(&_cond);
}
~ThreadPool() {
pthread_mutex_destroy(&_mutex);
pthread_cond_destroy(&_cond);
for (const auto& t : _threads)
delete t;
}
static ThreadPool<T>* getInstance() {
if (nullptr == tp) {
_singlock.lock();
if (nullptr == tp) {
tp = new ThreadPool<T>();
}
_singlock.unlock();
}
return tp;
}
private:
int _num;
std::vector<Thread*> _threads;
std::queue<T> _task_queue;
pthread_mutex_t _mutex;
pthread_cond_t _cond;
static ThreadPool<T>* tp;
static std::mutex _singlock;
};
template <class T>
ThreadPool<T>* ThreadPool<T>::tp = nullptr;
template <class T>
std::mutex ThreadPool<T>::_singlock;
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
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
上次更新: 2025/09/03, 18:26:17