-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.cpp
71 lines (55 loc) · 1.48 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
#include <iostream>
#include <Promise.h>
void runAsync(int i, PromiseContext& promises)
{
auto async = promises.CreateAsync<std::string, std::string>(
[](const TPromise<std::string, std::string>::HandlerPtr& h) {
for (int i = 0; i < 100; ++i) {
if (h->IsCanceled()) {
std::cout << "async canceled" << std::endl;
return;
}
if (i % 10 == 0) {
h->Progress(i);
}
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
h->Resolve("async resolved");
}
);
async->Then(
[i](const std::string& result) {
std::cout << result << " " << i << " in thread id " << std::this_thread::get_id() << std::endl;
},
[i](int progress) {
std::cout << i << " async progress " << progress << "%" << std::endl;
}
);
}
int main()
{
std::cout << "Hello World! main thread id " << std::this_thread::get_id() << std::endl;
PromiseContext promises;
std::vector<TPromise<std::string, std::string>::HandlerPtr> list;
{
auto p = promises.Create<std::string, std::string>(
[&list](const TPromise<std::string, std::string>::HandlerPtr& h) {
list.push_back(h);
});
p->Then([](const std::string& result) {
std::cout << result << std::endl;
});
//p->Cancel();
}
runAsync(0, promises);
std::thread t([&list]() {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
for (const auto& h : list) {
if (h->IsCanceled()) {
std::cout << "is canceled" << std::endl;
}
h->Resolve("ok");
}
});
t.join();
}