-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpool_callable.cpp
More file actions
76 lines (61 loc) · 1.97 KB
/
Copy pathpool_callable.cpp
File metadata and controls
76 lines (61 loc) · 1.97 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
#include <thread_pool/thread_pool.h>
#include <functional>
#include <future>
#include <iostream>
#include <string>
void normal_function(int x)
{
std::cout << "normal_function: " << x << '\n';
}
struct MyClass
{
void member_function(int y)
{
std::cout << "MyClass::member_function: " << y << '\n';
}
int add(int a, int b)
{
return a + b;
}
};
struct Functor
{
void operator()(const std::string &msg) const
{
std::cout << "Functor called with: " << msg << '\n';
}
};
int main()
{
abin::threadpool pool(4);
// 提交一个普通函数
pool.submit(normal_function, 42);
// 提交一个无捕获 lambda
pool.submit([] { std::cout << "lambda no capture\n"; });
// 提交一个有捕获 lambda
int value = 99;
pool.submit([value] { std::cout << "lambda with capture: " << value << "\n"; });
// 提交成员函数, 使用lambda
MyClass obj;
pool.submit([&obj] { obj.member_function(123); });
// 提交成员函数, 使用 std::mem_fn
std::future<int> ret = pool.submit(std::mem_fn(&MyClass::add), &obj, 3, 4);
std::cout << "add result1: " << ret.get() << "\n";
// 提交成员函数, 使用 std::bind
std::future<int> fut_add = pool.submit(std::bind(&MyClass::add, &obj, 2, 3));
std::cout << "add result2: " << fut_add.get() << "\n";
// 提交一个函数对象(仿函数)
Functor f;
pool.submit(f, "hello functor");
// 使用 std::bind 提交
auto bound = std::bind(&MyClass::add, &obj, 5, 6);
std::future<int> fut_bound = pool.submit(bound);
std::cout << "bound result: " << fut_bound.get() << "\n";
// 提交一个 std::packaged_task(注意: 低版本msvc可能报错)
std::packaged_task<std::string()> task([] { return std::string("from packaged_task"); });
std::future<std::string> fut_str = task.get_future();
pool.submit(std::move(task)); // 必须 move
std::cout << "packaged_task result: " << fut_str.get() << "\n";
pool.wait_all(); // 等待任务完成
std::cout << "===All tasks completed.===\n";
}