| 66 | } |
| 67 | |
| 68 | cobalt::main co_main(int argc, char *argv []) |
| 69 | { |
| 70 | // a very simple thread pool |
| 71 | std::vector<cobalt::thread> thrs; |
| 72 | const std::size_t n = 4u; |
| 73 | |
| 74 | request_channel rc{co_await cobalt::this_coro::executor}; |
| 75 | for (auto i = 0u; i < n; i++) |
| 76 | thrs.push_back(worker(rc)); |
| 77 | |
| 78 | try |
| 79 | { |
| 80 | // this is an over simplification, but emulated multiple pieces of |
| 81 | // code in the single threaded environment offloading work to the thread. |
| 82 | co_await cobalt::join( |
| 83 | work(rc, 0, 10, 32), |
| 84 | work(rc, 10, 20, 22), |
| 85 | work(rc, 50, 60, -18) |
| 86 | ); |
| 87 | |
| 88 | } |
| 89 | catch(std::exception & e) |
| 90 | { |
| 91 | printf("Completed with exception %s\n", e.what()); |
| 92 | } |
| 93 | // closing the channel will cause the threads to complete |
| 94 | rc.close(); |
| 95 | // wait them so they don't leak. |
| 96 | co_await cobalt::join(thrs); |
| 97 | co_return 0; |
| 98 | } |