|
1 |
| -// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]> |
2 |
| -// |
3 |
| -// SPDX-License-Identifier: MIT |
4 |
| - |
5 |
| -#include <boost/cobalt.hpp> |
6 |
| - |
7 |
| -#include <boost/asio/as_tuple.hpp> |
8 |
| -#include <boost/asio/experimental/concurrent_channel.hpp> |
9 |
| -#include <boost/asio/redirect_error.hpp> |
10 |
| -#include <boost/asio/this_coro.hpp> |
11 |
| - |
12 |
| -namespace cobalt = boost::cobalt; |
13 |
| -using boost::system::error_code; |
14 |
| - |
15 |
| -template <typename Signature> |
16 |
| -using cchannel = boost::asio::experimental::concurrent_channel<Signature>; |
17 |
| - |
18 |
| -// this is a function doing some CPU heavy work that should be offloaded onto a |
19 |
| -// thread |
20 |
| -inline cobalt::promise<int> cpu_intense_work(int a, int b) { co_return a + b; } |
21 |
| - |
22 |
| -// this channel is used to send a response to completed work |
23 |
| -using response_channel = cchannel<void(std::exception_ptr, int)>; |
24 |
| -// this channel is used to send a request to a working thread |
25 |
| -using function_handler = void(error_code, int, int, response_channel* res); |
26 |
| - |
27 |
| -using request_channel = cchannel<function_handler>; |
28 |
| - |
29 |
| -// the worker wrapper |
30 |
| -inline cobalt::thread worker(request_channel& work) { |
31 |
| - while (work.is_open()) { |
32 |
| - auto [ec, a, b, respond_to] = |
33 |
| - co_await work.async_receive(boost::asio::as_tuple(cobalt::use_op)); |
34 |
| - if (ec) // done, ignore. in our code this is only triggered by closing the |
35 |
| - // channel |
36 |
| - break; |
37 |
| - |
38 |
| - // to emulate this being like awaiting on the same thread, we also deliver |
39 |
| - // an exception. |
40 |
| - std::exception_ptr ep; |
41 |
| - int res = 0; |
42 |
| - try { |
43 |
| - res = co_await cpu_intense_work(a, b); |
44 |
| - } catch (...) { |
45 |
| - // this way exception get sent to the awaiting coro as if it was a call. |
46 |
| - ep = std::current_exception(); |
47 |
| - } |
48 |
| - // send the response. If the channel is closed, the program will terminate! |
49 |
| - co_await respond_to->async_send( |
50 |
| - ep, res, boost::asio::redirect_error(cobalt::use_op, ec)); |
51 |
| - } |
52 |
| -} |
53 |
| - |
54 |
| -inline cobalt::promise<void> work(request_channel& rc, |
55 |
| - int min_a, |
56 |
| - int max_a, |
57 |
| - int b) { |
58 |
| - response_channel res{co_await cobalt::this_coro::executor}; |
59 |
| - for (int a = min_a; a <= max_a; a++) { |
60 |
| - // the following two calls offload the work to another thread. |
61 |
| - co_await rc.async_send(error_code{}, a, b, &res, cobalt::use_op); |
62 |
| - int c = co_await res.async_receive( |
63 |
| - cobalt::use_op); // may throw if working thread has an exception |
64 |
| - printf("The CPU intensive result of adding %d to %d, is %d\n", a, b, c); |
65 |
| - } |
66 |
| -} |
67 |
| - |
68 |
| -cobalt::main co_main(int, char**) { |
69 |
| - // a very simple thread pool |
70 |
| - std::vector<cobalt::thread> thrs; |
71 |
| - const std::size_t n = 4u; |
72 |
| - |
73 |
| - request_channel rc{co_await cobalt::this_coro::executor}; |
74 |
| - for (auto i = 0u; i < n; i++) thrs.push_back(worker(rc)); |
75 |
| - |
76 |
| - try { |
77 |
| - // this is an over simplification, but emulated multiple pieces of |
78 |
| - // code in the single threaded environment offloading work to the thread. |
79 |
| - co_await cobalt::join( |
80 |
| - work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
81 |
| - work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
82 |
| - work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
83 |
| - work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
84 |
| - work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
85 |
| - work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18)); |
86 |
| - |
87 |
| - } catch (std::exception& e) { |
88 |
| - printf("Completed with exception %s\n", e.what()); |
89 |
| - } |
90 |
| - // closing the channel will cause the threads to complete |
91 |
| - rc.close(); |
92 |
| - // wait them so they don't leak. |
93 |
| - co_await cobalt::join(thrs); |
94 |
| - co_return 0; |
95 |
| -} |
| 1 | +// SPDX-FileCopyrightText: © 2024 Melg Eight <[email protected]> |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +#include <boost/cobalt.hpp> |
| 6 | + |
| 7 | +#include <boost/asio/as_tuple.hpp> |
| 8 | +#include <boost/asio/experimental/concurrent_channel.hpp> |
| 9 | +#include <boost/asio/redirect_error.hpp> |
| 10 | +#include <boost/asio/this_coro.hpp> |
| 11 | + |
| 12 | +namespace cobalt = boost::cobalt; |
| 13 | +using boost::system::error_code; |
| 14 | + |
| 15 | +template <typename Signature> |
| 16 | +using cchannel = boost::asio::experimental::concurrent_channel<Signature>; |
| 17 | + |
| 18 | +// this is a function doing some CPU heavy work that should be offloaded onto a |
| 19 | +// thread |
| 20 | +inline cobalt::promise<int> cpu_intense_work(int a, int b) { co_return a + b; } |
| 21 | + |
| 22 | +// this channel is used to send a response to completed work |
| 23 | +using response_channel = cchannel<void(std::exception_ptr, int)>; |
| 24 | +// this channel is used to send a request to a working thread |
| 25 | +using function_handler = void(error_code, int, int, response_channel* res); |
| 26 | + |
| 27 | +using request_channel = cchannel<function_handler>; |
| 28 | + |
| 29 | +// the worker wrapper |
| 30 | +inline cobalt::thread worker(request_channel& work) { |
| 31 | + while (work.is_open()) { |
| 32 | + auto [ec, a, b, respond_to] = |
| 33 | + co_await work.async_receive(boost::asio::as_tuple(cobalt::use_op)); |
| 34 | + if (ec) // done, ignore. in our code this is only triggered by closing the |
| 35 | + // channel |
| 36 | + break; |
| 37 | + |
| 38 | + // to emulate this being like awaiting on the same thread, we also deliver |
| 39 | + // an exception. |
| 40 | + std::exception_ptr ep; |
| 41 | + int res = 0; |
| 42 | + try { |
| 43 | + res = co_await cpu_intense_work(a, b); |
| 44 | + } catch (...) { |
| 45 | + // this way exception get sent to the awaiting coro as if it was a call. |
| 46 | + ep = std::current_exception(); |
| 47 | + } |
| 48 | + // send the response. If the channel is closed, the program will terminate! |
| 49 | + co_await respond_to->async_send( |
| 50 | + ep, res, boost::asio::redirect_error(cobalt::use_op, ec)); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +inline cobalt::promise<void> work(request_channel& rc, |
| 55 | + int min_a, |
| 56 | + int max_a, |
| 57 | + int b) { |
| 58 | + response_channel res{co_await cobalt::this_coro::executor}; |
| 59 | + for (int a = min_a; a <= max_a; a++) { |
| 60 | + // the following two calls offload the work to another thread. |
| 61 | + co_await rc.async_send(error_code{}, a, b, &res, cobalt::use_op); |
| 62 | + int c = co_await res.async_receive( |
| 63 | + cobalt::use_op); // may throw if working thread has an exception |
| 64 | + printf("The CPU intensive result of adding %d to %d, is %d\n", a, b, c); |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +cobalt::main co_main(int, char**) { |
| 69 | + // a very simple thread pool |
| 70 | + std::vector<cobalt::thread> thrs; |
| 71 | + const std::size_t n = 4u; |
| 72 | + |
| 73 | + request_channel rc{co_await cobalt::this_coro::executor}; |
| 74 | + for (auto i = 0u; i < n; i++) thrs.push_back(worker(rc)); |
| 75 | + |
| 76 | + try { |
| 77 | + // this is an over simplification, but emulated multiple pieces of |
| 78 | + // code in the single threaded environment offloading work to the thread. |
| 79 | + co_await cobalt::join( |
| 80 | + work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
| 81 | + work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
| 82 | + work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
| 83 | + work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
| 84 | + work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18), |
| 85 | + work(rc, 0, 10, 32), work(rc, 10, 20, 22), work(rc, 50, 60, -18)); |
| 86 | + |
| 87 | + } catch (std::exception& e) { |
| 88 | + printf("Completed with exception %s\n", e.what()); |
| 89 | + } |
| 90 | + // closing the channel will cause the threads to complete |
| 91 | + rc.close(); |
| 92 | + // wait them so they don't leak. |
| 93 | + co_await cobalt::join(thrs); |
| 94 | + co_return 0; |
| 95 | +} |
0 commit comments