Is it possible to know when all pending asynchronous tasks in strand
are complete? Just like thread.join()
does in the following example:
io_service service;
std::thread thread([&](){ service.run(); });
service.post(some_function);
service.post(another_function);
service.stop();
thread.join();
It would be useful for efficient execution of multiple tasks on multiple threads. Each task is more complex than
ordinary function and has it's own strand. But I've found no way to wait for strand until it has 'work'.
I've tried to post
to a strand finalizing handler in hope it would be called last, but the order of handlers in strand is undefined, so it fires immediately.
The code with strand would be:
io_service service;
strand<io_context::executor_type> strand(make_strand(service));
std::thread thread([&](){ service.run(); });
post(strand, some_function);
post(strand, another_function);
// here we want to wait for strand to complete pending tasks
// somewhere else later
service.stop();
thread.join();
Thank you.