YACLib
C++ library for concurrent tasks execution
Loading...
Searching...
No Matches
when_all.cpp
Go to the documentation of this file.
1/**
2 * \example when_all.cpp
3 * Simple WhenAll examples
4 */
10
11#include <chrono>
12#include <iostream>
13#include <type_traits>
14#include <utility>
15#include <vector>
16
17#include <gtest/gtest.h>
18
19namespace test {
20namespace {
21
22using namespace std::chrono_literals;
23
24// All combinator:
25// std::vector<Future<T>> -> Future<std::vector<T>>
26
27TEST(Example, WhenAll) {
29
30 std::vector<yaclib::FutureOn<int>> futs;
31
32 // Run sync computations in parallel
33
34 futs.reserve(5);
35 for (int i = 0; i < 5; ++i) {
36 futs.push_back(yaclib::Run(tp, [i]() -> int {
37 return i;
38 }));
39 }
40
41 // Parallel composition
42 // All combinator: std::vector<Future<T>> -> Future<std::vector<T>>
43 // Non-blocking!
44 yaclib::Future<std::vector<int>> all = WhenAll(futs.begin(), futs.size());
45
46 // Blocks
47 std::vector<int> ints = std::move(all).Get().Ok();
48
49 std::cout << "All ints: ";
50 for (auto v : ints) {
51 std::cout << v << ", ";
52 }
53 std::cout << std::endl;
54 tp.Stop();
55 tp.Wait();
56}
57
58} // namespace
59} // namespace test
TODO(kononovk) Doxygen docs.
Provides a mechanism to access the result of async operations.
Definition future.hpp:199
auto Run(Func &&f)
Execute Callable func on Inline executor.
Definition run.hpp:38
TEST(Example, HelloWorld)
Definition simple.cpp:27