Aleph-w 3.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
experimental_async_example.cc
Go to the documentation of this file.
1
18#include <experimental_async.H>
19#include <thread_pool.H>
20
21#include <iostream>
22#include <string>
23
24#if ALEPH_HAS_EXPERIMENTAL_ASYNC
25# include <coroutine>
26# include <future>
27#endif
28
29using namespace Aleph;
30
31#if ALEPH_HAS_EXPERIMENTAL_ASYNC
32namespace
33{
34 template <typename T>
35 class sync_task
36 {
37 std::future<T> future_;
38
39 public:
40 struct promise_type
41 {
42 std::promise<T> promise_;
43
45 {
46 return sync_task(promise_.get_future(),
47 std::coroutine_handle<promise_type>::from_promise(*this));
48 }
49
50 std::suspend_never initial_suspend() noexcept { return {}; }
51 std::suspend_always final_suspend() noexcept { return {}; }
52
53 void return_value(T value)
54 {
55 promise_.set_value(std::move(value));
56 }
57
59 {
60 promise_.set_exception(std::current_exception());
61 }
62 };
63
64 private:
65 std::coroutine_handle<promise_type> handle_;
66
67 sync_task(std::future<T> future,
68 std::coroutine_handle<promise_type> handle) noexcept
69 : future_(std::move(future)), handle_(handle) {}
70
71 public:
72 sync_task(const sync_task &) = delete;
73 sync_task & operator = (const sync_task &) = delete;
74
76 : future_(std::move(other.future_)), handle_(other.handle_)
77 {
78 other.handle_ = {};
79 }
80
81 sync_task & operator = (sync_task && other) noexcept
82 {
83 if (this == &other)
84 return *this;
85 if (handle_)
86 handle_.destroy();
87 future_ = std::move(other.future_);
88 handle_ = other.handle_;
89 other.handle_ = {};
90 return *this;
91 }
92
94 {
95 if (handle_)
96 handle_.destroy();
97 }
98
99 T get()
100 {
101 return future_.get();
102 }
103 };
104
106 {
107 const int left = co_await experimental::schedule(pool, [] { return 20; });
108 const int right = co_await experimental::schedule(pool, [left] {
109 return left + 22;
110 });
111 co_return right;
112 }
113} // namespace
114#endif
115
116int main()
117{
118#if ALEPH_HAS_EXPERIMENTAL_ASYNC
119 ThreadPool pool(4);
120
121 auto greeting = experimental::schedule(pool, [] {
122 return std::string("hello from the experimental async bridge");
123 });
124
125 std::cout << greeting.get() << "\n";
126 std::cout << "coroutine chain result: " << coroutine_chain(pool).get() << "\n";
127
128 auto default_result = experimental::schedule([] { return 7 * 6; });
129 std::cout << "default pool result: " << default_result.get() << "\n";
130#else
131 std::cout
132 << "Experimental async bridge is disabled.\n"
133 << "Reconfigure with -DALEPH_ENABLE_EXPERIMENTAL_ASYNC=ON and use C++20.\n";
134#endif
135
136 return 0;
137}
A reusable thread pool for efficient parallel task execution.
Opt-in experimental coroutine-friendly bridge over ThreadPool.
Main namespace for Aleph-w library functions.
Definition ah-arena.H:89
Divide_Conquer_DP_Result< Cost > divide_and_conquer_partition_dp(const size_t groups, const size_t n, Transition_Cost_Fn transition_cost, const Cost inf=dp_optimization_detail::default_inf< Cost >())
Optimize partition DP using divide-and-conquer optimization.
std::decay_t< typename HeadC::Item_Type > T
Definition ah-zip.H:105
A modern, efficient thread pool for parallel task execution.