-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
99 lines (79 loc) · 2.91 KB
/
main.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
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
#include "threadpool.hpp"
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
void fun1(int slp) {
cout << " hello, fun1 ! " << this_thread::get_id() << endl;
if (slp > 0) {
cout << "============ fun1 sleep " << slp << " =========== " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::milliseconds(slp));
// Sleep(slp);
}
}
struct gfun {
int operator()(int n) {
cout << n << " hello, gfun ! " << this_thread::get_id() << endl;
return 42;
}
};
class A {
public:
static int Afun(int n = 0) {
cout << n << " hello, Afun ! " << this_thread::get_id() << endl;
return n;
}
static string Bfun(int n, string str, char c) {
cout << n << " hello, Bfun ! " << str.c_str() << " " << int(c) << " " << this_thread::get_id() << endl;
return str;
}
};
int main() {
try {
threadpool tp{ 50 };
A a;
future<void> ff = tp.commit(fun1, 42);
future<int> fg = tp.commit(gfun{}, 42);
future<int> gg = tp.commit(a.Afun, 42); // ide 提示错误?但可以编译运行
future<string> gh = tp.commit(a.Bfun, 42, "42", 42);
future<string> fh = tp.commit([]() -> string {
cout << " hello, fh ! " << this_thread::get_id() << endl;
return "hello, fh ret!";
});
cout << " ======= sleep ======= " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::milliseconds(42));
for (int i = 0; i < 50; ++i) {
tp.commit(fun1, i * 42);
}
cout << " ======= commit all ====== " << this_thread::get_id() << " idlsize = " << tp.idlCount() << endl;
cout << " ======= sleep ======= " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::seconds(3));
ff.get(); // 调用.get()获取返回值会等待线程执行完,获取返回值
cout << fg.get() << " " << fh.get().c_str() << " " << this_thread::get_id() << endl;
cout << " ======= sleep ========= " << this_thread::get_id() << endl;
this_thread::sleep_for(chrono::seconds(3));
cout << " ======= fun1,55 ========= " << this_thread::get_id() << endl;
tp.commit(fun1, 55).get(); //调用.get()获取返回值会等待线程执行完
cout << "end... " << this_thread::get_id() << endl;
threadpool pool(4);
vector<future<int>> results;
for (int i = 0; i < 8; ++i) {
results.emplace_back(
pool.commit([i] {
cout << "hello " << i << endl;
this_thread::sleep_for(chrono::seconds(1));
cout << "world " << i << endl;
return i * i;
})
);
}
cout << " ======= commit all2 ========= " << this_thread::get_id() << endl;
for (auto && result : results)
cout << result.get() << ' ';
cout << endl;
}
catch (exception& e) {
cout << "some unhappy happened... " << this_thread::get_id() << e.what() << endl;
}
return 0;
}