
My problem is with the function that is to be executed last: how can I wait for that to be executed before stopping the io_service and joining the thread?
Usually you don't have to stop io_service explicitly - when it runs
out of work, it stops by its own. So if all you need is to perform
several functions/functors on io_service thread - just post them to
io_service, and join io_service thread(s):
// pseudo-code!
{
asio::io_service io;
io_.post(functor1);
io_.post(functor2);
thread t(&io_service::run, &io);
t.join(); // when io_service runs out of jobs, run() exits.
}
If you need to get more control over io_service::run "lifetime", you
can use work object, like in your example:
// pseudo-code!
{
asio::io_service io;
shared_ptr