-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecution_phase3_example.cpp
More file actions
31 lines (25 loc) · 1022 Bytes
/
Copy pathexecution_phase3_example.cpp
File metadata and controls
31 lines (25 loc) · 1022 Bytes
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
#include <execution>
#include "example_support.hpp"
#include <tuple>
namespace ex = std::execution;
int main() {
// when_all: run two senders, combine results
auto result = ex::sync_wait(ex::when_all(ex::just(42), ex::just(3.14)));
forge_example::require(result.has_value());
auto [a, b] = *result;
forge_example::require(a == 42);
forge_example::require(b > 3.0);
// split: reuse a single sender result
auto shared = ex::split(ex::just(100) | ex::then([](int x) { return x * 2; }));
forge_example::require(std::get<0>(*ex::sync_wait(shared)) == 200);
forge_example::require(std::get<0>(*ex::sync_wait(shared)) == 200);
// bulk: serial iteration
int sum = 0;
ex::sync_wait(ex::just(0) | ex::bulk(5, [&sum](int i, int&) { sum += i; }));
forge_example::require(sum == 0+1+2+3+4);
// spawn: fire and forget under a scope token
ex::simple_counting_scope scope;
ex::spawn(ex::just(), scope.get_token());
ex::sync_wait(scope.join());
return 0;
}