-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathstop_token.cc
61 lines (53 loc) · 1.63 KB
/
stop_token.cc
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
#include <iostream>
#include <mutex>
#include <thread>
#include <vector>
#include "src/cancel.h"
#include "src/io_util.h"
#include "src/macros.h"
#include "src/thread_pool.h"
using namespace arrow;
template <typename T>
static T add(T x, T y) {
std::this_thread::sleep_for(std::chrono::seconds(2));
return x + y;
}
template <typename T>
static T sub(T x, T y) {
std::this_thread::sleep_for(std::chrono::seconds(2));
return x - y;
}
template <typename T>
static T mul(T x, T y) {
std::this_thread::sleep_for(std::chrono::seconds(2));
return x * y;
}
void status_callback(const Status& status) {
std::cout << "status_callback: " << status.ToString() << std::endl;
}
int main() {
arrow::StopSource stop_source;
arrow::StopToken stop_token = stop_source.token();
auto threadPool = GetCpuThreadPool();
int a = 10;
int b = 20;
std::vector<std::future<int>> futures;
// Submit multiple tasks
auto add_fut = threadPool->Submit(arrow::TaskHints{}, stop_token, status_callback, [a, b]() { return add(a, b); });
auto sub_fut = threadPool->Submit(stop_token, [a, b]() { return sub(a, b); });
auto mul_fut = threadPool->Submit(stop_token, [a, b]() { return mul(a, b); });
futures.push_back(std::move(add_fut));
futures.push_back(std::move(sub_fut));
futures.push_back(std::move(mul_fut));
stop_source.RequestStop();
// Retrieve the results
for (auto& future : futures) {
try {
int result = future.get();
std::cout << "Result: " << result << std::endl;
} catch (const std::exception& e) { /* will throw error. */
std::cerr << "Exception occurred: " << e.what() << std::endl;
}
}
return 0;
}