the worker wrapper
| 29 | |
| 30 | // the worker wrapper |
| 31 | cobalt::thread worker(request_channel & work) |
| 32 | { |
| 33 | while (work.is_open()) |
| 34 | { |
| 35 | auto [ec, a, b, respond_to] = co_await work.async_receive(boost::asio::as_tuple(cobalt::use_op)); |
| 36 | if (ec) // done, ignore. in our code this is only triggered by closing the channel |
| 37 | break; |
| 38 | |
| 39 | // to emulate this being like awaiting on the same thread, we also deliver an exception. |
| 40 | std::exception_ptr ep; |
| 41 | int res = 0; |
| 42 | try |
| 43 | { |
| 44 | res = co_await cpu_intense_work(a, b); |
| 45 | } |
| 46 | catch(...) |
| 47 | { |
| 48 | // this way exception get sent to the awaiting coro as if it was a call. |
| 49 | ep = std::current_exception(); |
| 50 | } |
| 51 | // send the response. If the channel is closed, the program will terminate! |
| 52 | co_await respond_to->async_send(ep, res, boost::asio::redirect_error(cobalt::use_op, ec)); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | cobalt::promise<void> work(request_channel & rc, int min_a, int max_a, int b) |
| 57 | { |
no test coverage detected