asio icon indicating copy to clipboard operation
asio copied to clipboard

asio::yield: Sending Blocking work to threadpool and waiting for threadpool to yield back results so the io_ctx remains unblocked.

Open WarrenN1 opened this issue 2 years ago • 0 comments

I have 2 features in my server:

  1. an IO Loop
  2. a Thread Pool I would like to launch a process on the IO Loop, and then have it initiate some work on the threadpool, resume processing other IO tasks, and then when the threadpool finishes the work have it yield the value back to the IO task which will continue. Honestly, the documentation has been rough and the error messages given by GCC have been slightly less than helpful. Any help would be deeply appreciated.
void handle_request(asio::io_context& tp_ctx)
{
auto result;
std::cerr << "Some IO loop appropriate work" << "\n";
spawn(tp_ctx, blocking_work(yield[result]));
std:cerr << result << "\n";
}
void main()
{
    //1a. Build IO Threadpool required for all I/O Work
    asio::io_context io_ctx{num_io_threads};
    //1b. Build Threadpool required for all Blocking Work
    asio::io_context tp_ctx{num_tp_threads};
    
    asio::spawn(io_ctx,
                         std::bind(&handle_request,
                                         std::ref(tp_ctx))
                         )
tp_ctx.run()
io_ctx.run()
}

The way my brain is imagining things is as follows:

  1. launch "handle_request" on io_ctx
  2. Does some work on io_ctx
  3. Launches some blocking_work on the threadpool (tp_ctx) and suspends the handle request until threadpool sets the yield value. io_ctx remains unblocked.
  4. When thread pool finishes it sets the yield value (which exists on the io_ctx) on the threadpool executor that actually did the work.
  5. The io_ctx uses the actual result.

I would much prefer using a threadpool setup rather than 2 io contexts, but io_ctx has some nice built-in threadpool management I can use. I am defenitely open to new ideas though given I am new to this.

I am working on a personal project all on my own and any help is deeply appreciated more than you know.

WarrenN1 avatar Jun 07 '22 06:06 WarrenN1