MCPcopy Create free account
hub / github.com/boostorg/cobalt / worker

Function worker

example/thread.cpp:31–54  ·  view source on GitHub ↗

the worker wrapper

Source from the content-addressed store, hash-verified

29
30// the worker wrapper
31cobalt::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
56cobalt::promise<void> work(request_channel & rc, int min_a, int max_a, int b)
57{

Callers 1

co_mainFunction · 0.85

Calls 3

as_tupleFunction · 0.85
cpu_intense_workFunction · 0.70
is_openMethod · 0.45

Tested by

no test coverage detected