-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
103 lines (77 loc) · 2.29 KB
/
Copy pathmain.cpp
File metadata and controls
103 lines (77 loc) · 2.29 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <future>
#include "continuation.h"
struct timed_execution
{
using func_type = std::function<void(void)>;
timed_execution(func_type func, const std::chrono::milliseconds period)
: func_(func)
, period_(period)
{
}
void run()
{
thread_ = std::thread(std::bind(&timed_execution::thread_func,this));
}
private:
void thread_func()
{
std::this_thread::sleep_for(period_);
func_();
}
func_type func_;
const std::chrono::milliseconds period_;
std::thread thread_;
};
void print()
{
std::cout << "Then this" << std::flush;
}
void print2()
{
std::cout << "Then this2" << std::flush;
}
class timed_continuator : public continuation<int, std::string, int>
{
public:
timed_continuator(void (*fun)(void), std::string n = "q"):
n {n},
func {fun}
{
}
std::future<int> run_impl(std::string s, int) override
{
//t.run();
func();
std::this_thread::sleep_for(std::chrono::milliseconds(1750));
auto val = invoke_handler(s + "i", 13);
return std::async(std::launch::deferred, [](std::future<int>&& f){ return f.get()+101;}, std::move(val));
//return std::async(std::launch::deferred, [](){return 20;});
}
private:
std::string n;
void (*func)(void);
};
int main()
{
timed_continuator t(print);
std::cout << "\n";
auto f = timed_continuator::pointer (new timed_continuator(print2, ""));
auto f2 = timed_continuator::pointer (new timed_continuator(print, ""));
auto y = t
| timed_continuator::pointer { new timed_continuator(print2, "") }
| (std::move(f) | std::move(f2))
<< timed_continuator::condition ([](std::string s, int) { return s.length() < 3; })
| timed_continuator::pointer { new timed_continuator(print, "")}
;
y->and_then([](std::string s, int) {
std::cout << s << std::flush; return 97; });
auto fut = y->run("", 0);
std::cout << "This first" << std::flush;
std::this_thread::sleep_for(std::chrono::seconds(30));
std::cout << "\n" << fut.get();
return 0;
}