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

Simple WhenAny examples.

Simple WhenAny examples

/**
* \example when_any.cpp
* Simple WhenAny examples
*/
#include <chrono>
#include <iostream>
#include <type_traits>
#include <utility>
#include <vector>
#include <gtest/gtest.h>
namespace test {
namespace {
using namespace std::chrono_literals;
// Any combinator:
// std::vector<Future<T>> -> Future<T>
TEST(Example, WhenAny) {
std::vector<yaclib::FutureOn<int>> futs;
// Run sync computations in parallel
for (int i = 0; i < 5; ++i) {
futs.push_back(yaclib::Run(tp, [i]() -> int {
return i;
}));
}
// Parallel composition
// Any combinator: std::vector<Future<T>> -> Future<T>
// Non-blocking!
yaclib::Future<int> any = WhenAny(futs.begin(), futs.size());
// First value
std::cout << "Any value: " << std::move(any).Get().Ok() << std::endl;
tp.Stop();
tp.Wait();
}
} // namespace
} // namespace test
TODO(kononovk) Doxygen docs.
Provides a mechanism to access the result of async operations.
Definition: future.hpp:155
auto Run(Func &&f)
Execute Callable func on Inline executor.
Definition: run.hpp:38
auto WhenAny(It begin, std::size_t count)
Create Future that is ready when any of futures is ready.
Definition: when_any.hpp:26
TEST(Example, HelloWorld)
Definition: simple.cpp:27