YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
simple.cpp
Go to the documentation of this file.
1/**
2 * \example simple.cpp
3 * Simple Future examples
4 */
5
11#include <yaclib/exe/strand.hpp>
15
16#include <chrono>
17#include <exception>
18#include <iostream>
19#include <string>
20#include <type_traits>
21#include <utility>
22
23#include <gtest/gtest.h>
24
25using namespace std::chrono_literals;
26
27TEST(Example, HelloWorld) {
28 auto [f, p] = yaclib::MakeContract<int>();
29
30 std::move(p).Set(42);
31
32 EXPECT_TRUE(f.Ready());
33
34 yaclib::Result<int> result = std::move(f).Get();
35
36 EXPECT_EQ(std::move(result).Ok(), 42);
37}
38
39TEST(Example, Detach) {
41
42 auto f = yaclib::Run(tp, [] {
43 return 42;
44 });
45
46 std::move(f).Detach([](yaclib::Result<int> result) {
47 std::cout << "Async result from thread pool: " << std::move(result).Ok() << std::endl;
48 });
49
50 EXPECT_TRUE(!f.Valid());
51
52 tp.SoftStop();
53 tp.Wait();
54}
55
56TEST(Example, Then) {
58
59 auto compute = [] {
60 return 42;
61 };
62
63 auto process = [](int r) {
64 return r + 1;
65 };
66
67 yaclib::FutureOn<int> f1 = yaclib::Run(tp, compute);
68
69 yaclib::FutureOn<int> f2 = std::move(f1).Then(process);
70
71 EXPECT_TRUE(!f1.Valid());
72
73 std::cout << "process(compute()) -> " << std::move(f2).Get().Ok() << std::endl;
74
75 tp.SoftStop();
76 tp.Wait();
77}
78
79TEST(Example, Pipeline) {
81
82 // Pipeline stages:
83
84 auto first = []() -> int {
85 return 42;
86 };
87
88 auto second = [](int r) {
89 return r * 2;
90 };
91
92 auto third = [](int r) {
93 return r + 1;
94 };
95
96 auto fourth = [](int r) {
97 return std::to_string(r);
98 };
99
100 auto last = [](yaclib::Result<std::string> r) {
101 std::cout << "Pipeline result: <" << std::move(r).Ok() << ">" << std::endl;
102 };
103
104 // Chain pipeline stages and run them in thread pool
105 yaclib::Run(tp, first).Then(second).Then(third).Then(fourth).Detach(last);
106
107 tp.SoftStop();
108 tp.Wait();
109}
110
112 public:
114 }
115
117 return yaclib::Run(*e_, [value]() {
118 return value + 1;
119 });
120 }
121
123 return yaclib::Run(*e_, [value]() {
124 return value * 2;
125 });
126 }
127
128 private:
130};
131
132TEST(Example, AsyncPipeline) {
134
135 CalculatorService calculator(&tp);
136
137 calculator.Increment(1)
138 .Then([&](int r) {
139 return calculator.Double(r);
140 })
141 .Then([&](int r) {
142 return calculator.Increment(r);
143 })
144 .Detach([](yaclib::Result<int> r) {
145 std::cout << "Result: " << std::move(r).Ok() << std::endl;
146 });
147
148 tp.SoftStop();
149 tp.Wait();
150}
151
152/**
153 \todo Add WhenAny example
154 */
155
156TEST(Example, Race) {
159
160 auto [f, p] = yaclib::MakeContract<int>();
161
162 yaclib::Run(tp, [p = std::move(p)]() mutable {
163 std::move(p).Set(42);
164 }).Detach();
165
166 std::move(f).Detach(tp2, [](yaclib::Result<int> /*r*/) {
167 std::cout << "Hello from the second thread pool!";
168 });
169
170 tp.SoftStop();
171 tp.Wait();
172 tp2.SoftStop();
173 tp2.Wait();
174}
175
176TEST(Example, StrandAsync) {
178 auto strand = yaclib::MakeStrand(&tp);
179
180 auto first = []() {
181 return 42;
182 };
183
184 auto second = [](int r) {
185 return r;
186 };
187
188 auto third = [](int r) {
189 return r + 1;
190 };
191
192 yaclib::Run(tp, first)
193 .Then(*strand, second) // Serialized
194 .Then(tp, third)
195 .Detach([](yaclib::Result<int> r) {
196 std::cout << "Final result: " << std::move(r).Value() << std::endl;
197 });
198
199 tp.SoftStop();
200 tp.Wait();
201}
yaclib::FutureOn< int > Increment(int value)
Definition simple.cpp:116
CalculatorService(yaclib::IExecutorPtr e)
Definition simple.cpp:113
yaclib::FutureOn< int > Double(int value)
Definition simple.cpp:122
TODO(kononovk) Doxygen docs.
bool Valid() const &noexcept
Check if this Future has Promise.
Definition future.hpp:53
Provides a mechanism to access the result of async operations.
Definition future.hpp:232
auto Then(Func &&f) &&
Attach the continuation func to *this.
Definition future.hpp:273
Encapsulated return value from caller.
Definition result.hpp:90
auto Run(Func &&f)
Execute Callable func on Inline executor.
Definition run.hpp:38
IExecutorPtr MakeStrand(IExecutorPtr e)
Strand is the asynchronous analogue of a mutex.
Definition strand.cpp:71
Contract< V, E > MakeContract()
Creates related future and promise.
Definition contract.hpp:25
TEST(Example, HelloWorld)
Definition simple.cpp:27