YACLib
C++ library for concurrent tasks execution
simple.cpp

Simple Future examples.

Simple Future examples

/**
* \example simple.cpp
* Simple Future examples
*/
#include <chrono>
#include <exception>
#include <iostream>
#include <string>
#include <type_traits>
#include <utility>
#include <gtest/gtest.h>
using namespace std::chrono_literals;
TEST(Example, HelloWorld) {
auto [f, p] = yaclib::MakeContract<int>();
std::move(p).Set(42);
EXPECT_TRUE(f.Ready());
yaclib::Result<int> result = std::move(f).Get();
EXPECT_EQ(std::move(result).Ok(), 42);
}
TEST(Example, Detach) {
auto f = yaclib::Run(tp, [] {
return 42;
});
std::move(f).Detach([](yaclib::Result<int> result) {
std::cout << "Async result from thread pool: " << std::move(result).Ok() << std::endl;
});
EXPECT_TRUE(!f.Valid());
tp.SoftStop();
tp.Wait();
}
TEST(Example, Then) {
auto compute = [] {
return 42;
};
auto process = [](int r) {
return r + 1;
};
yaclib::FutureOn<int> f2 = std::move(f1).Then(process);
EXPECT_TRUE(!f1.Valid());
std::cout << "process(compute()) -> " << std::move(f2).Get().Ok() << std::endl;
tp.SoftStop();
tp.Wait();
}
TEST(Example, Pipeline) {
// Pipeline stages:
auto first = []() -> int {
return 42;
};
auto second = [](int r) {
return r * 2;
};
auto third = [](int r) {
return r + 1;
};
auto fourth = [](int r) {
return std::to_string(r);
};
auto last = [](yaclib::Result<std::string> r) {
std::cout << "Pipeline result: <" << std::move(r).Ok() << ">" << std::endl;
};
// Chain pipeline stages and run them in thread pool
yaclib::Run(tp, first).Then(second).Then(third).Then(fourth).Detach(last);
tp.SoftStop();
tp.Wait();
}
public:
}
return yaclib::Run(*e_, [value]() {
return value + 1;
});
}
return yaclib::Run(*e_, [value]() {
return value * 2;
});
}
private:
};
TEST(Example, AsyncPipeline) {
CalculatorService calculator(&tp);
calculator.Increment(1)
.Then([&](int r) {
return calculator.Double(r);
})
.Then([&](int r) {
return calculator.Increment(r);
})
std::cout << "Result: " << std::move(r).Ok() << std::endl;
});
tp.SoftStop();
tp.Wait();
}
/**
\todo Add WhenAny example
*/
TEST(Example, Race) {
auto [f, p] = yaclib::MakeContract<int>();
yaclib::Run(tp, [p = std::move(p)]() mutable {
std::move(p).Set(42);
}).Detach();
std::move(f).Detach(tp2, [](yaclib::Result<int> /*r*/) {
std::cout << "Hello from the second thread pool!";
});
tp.SoftStop();
tp.Wait();
tp2.SoftStop();
tp2.Wait();
}
TEST(Example, StrandAsync) {
auto strand = yaclib::MakeStrand(&tp);
auto first = []() {
return 42;
};
auto second = [](int r) {
return r;
};
auto third = [](int r) {
return r + 1;
};
yaclib::Run(tp, first)
.Then(*strand, second) // Serialized
.Then(tp, third)
.Detach([](yaclib::Result<int> r) {
std::cout << "Final result: " << std::move(r).Value() << std::endl;
});
tp.SoftStop();
tp.Wait();
}
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_impl.hpp:18
Provides a mechanism to access the result of async operations.
Definition: future.hpp:185
auto Then(Func &&f) &&
Attach the continuation func to *this.
Encapsulated return value from caller.
Definition: result.hpp:78
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
TEST(Example, HelloWorld)
Definition: simple.cpp:27