Reputation: 2062
I run boost deadline_timer
and do async_wait
, but timer cancelled immediately. What i am doing wrong? I run ioService in my main file.
Thank you for any possible help
class A(boost::asio:io_service& ioService):
m_timer(ioService)
{
m_timer.expires_at(boost::posix_time::pos_infin);
m_timer.async_wait([this](const boost::system::error_code& ec)
{
std::cout << "Timer callback " << ec.message() << std::endl;
});
Upvotes: 1
Views: 879
Reputation: 393769
Check the lifetime of your A
object.
E.g. if you do this:
#include <boost/asio.hpp>
#include <iostream>
struct A {
A(boost::asio::io_service& ioService) : m_timer(ioService)
{
m_timer.expires_at(boost::posix_time::pos_infin);
m_timer.async_wait(
[this](const boost::system::error_code& ec) { std::cout << "Timer callback " << ec.message() << std::endl; }
);
}
boost::asio::deadline_timer m_timer;
};
int main()
{
boost::asio::io_service svc;
{
A a(svc);
}
svc.run();
}
The timer will have been canceled even before run()
is invoked.
The following will do what you expected
int main()
{
boost::asio::io_service svc;
{
A a(svc);
svc.run();
} // A destructed after `run()` completes
}
Upvotes: 2