-
Notifications
You must be signed in to change notification settings - Fork 17
/
threadpool.cpp
67 lines (57 loc) · 1.3 KB
/
threadpool.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
#include <iostream>
#include <functional>
#include <vector>
#include <thread>
using namespace std;
class Thread
{
public:
using PFUNC = function<void(int)>;
Thread(PFUNC pfunc, int id) : _pfunc(pfunc), _no(id) {}
thread start() {
thread t(_pfunc, _no);
return t;
}
private:
PFUNC _pfunc;
int _no;
};
class ThreadPool
{
public:
ThreadPool() {}
~ThreadPool() {
// 释放 Thread 对象占用的堆资源
for (int i = 0; i < _pool.size(); ++ i) {
delete _pool[i];
}
}
// 开启线程池
void startPool(int size)
{
for (int i = 0; i < size; ++ i) {
// Thread 参数是一个函数对象,可以使用 bind 绑定
_pool.push_back(new Thread(bind(&ThreadPool::runInThread, this, std::placeholders::_1), i));
}
for (int i = 0; i < size; ++ i) {
_handler.push_back(_pool[i]->start());
}
for (thread &t: _handler) {
t.join();
}
}
private:
vector<Thread *> _pool;
vector<thread> _handler;
void runInThread(int id)
{
std::this_thread::sleep_for(std::chrono::seconds(1));
cout << "call runInThread! id:" << id << endl;
}
};
int main()
{
ThreadPool pool;
pool.startPool(10);
return 0;
}