I execute a simple example as a test, I want execute a simple operation after 5 seconds. I am using the boost::deadline_timer with async_wait, but async_wait not wait asynchronously... This is the code:
void print(const boost::system::error_code& /*e*/)
{
std::cout << "Second Message\n";
}
int main()
{
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.async_wait(print);
io.run();
std::cout << "First Message\n";
return 0;
}
And this is output:
Second Message
First Message
There is an error because, the timer would have to wait in background and so to continue the execution of next instruction that is "cout<<"FirstMessage\n";"
The expected behavior is print "First Message" and after print "Second Message"
Thanks, I solved in this way:
boost::asio::io_service io;
boost::asio::deadline_timer t(io, boost::posix_time::seconds(5));
t.async_wait(print);
std::thread thread([&]{io.run();});