title: Why does boost asio function expires_from_now() cancel a deadline_timer?
body: <p>When I try to get (not set!) the current expiry time using
  boost expires_from_now() is seems to actually cancel the timer,
  yet it actually runs as expected, but does finally not call the handler.</p>
  
  <p>Or put in other words, when accessing a deadline_timer with expires_from_now()
  it will call the handler immediately and not call the handler when it expires.</p>
  
  <p>Please consider the following code and corresponding output:</p>
  
  <pre><code>#include &lt;boost/asio.hpp&gt; 
  #include &lt;boost/thread.hpp&gt; 
  #include &lt;boost/date_time/posix_time/posix_time.hpp&gt;
  #include &lt;boost/date_time/posix_time/posix_time_io.hpp&gt;
  #include &lt;iostream&gt; 
  
  using namespace boost::posix_time;
  using namespace std;
  
  void handler1(const boost::system::error_code &amp;ec) 
  { 
      if (ec == boost::asio::error::operation_aborted)
      {
          std::cout &lt;&lt; microsec_clock::local_time() &lt;&lt; " Handler1: Timer 1 was cancelled or retriggered." &lt;&lt; std::endl; 
      }
      else
      {
          std::cout &lt;&lt; microsec_clock::local_time() &lt;&lt; " Handler1: expired." &lt;&lt; std::endl; 
      }
  } 
  
  boost::asio::io_service io_service1; 
  
  class Mytimer {
  public:
      Mytimer();
      void startTimer();
      void runTimerThread();
      bool isRunning();
  private:
      bool m_isRunning;
      boost::asio::deadline_timer* m_pTimer;
      boost::thread* m_pThread;
  };
  
  Mytimer::Mytimer()
    : m_pTimer(NULL),
      m_pThread(NULL)
  {
      m_pTimer = new boost::asio::deadline_timer(io_service1); 
      m_pTimer-&gt;async_wait(handler1); 
  }
  
  void Mytimer::runTimerThread()
  {
      io_service1.run();
  }
  
  void Mytimer::startTimer()
  {
      m_pThread = new boost::thread(&amp;Mytimer::runTimerThread, this);
      m_pTimer-&gt;expires_from_now(boost::posix_time::seconds(10));
  }
  
  bool Mytimer::isRunning()
  {
  
      time_duration td = m_pTimer-&gt;expires_from_now();
      if (td.total_seconds() &gt; 0)
      {
          return true;
      }
      return false;
  }
  
  int main() 
  { 
      time_facet *facet = new time_facet("%Y%m%d %H:%M:%S%f");
      std::cout.imbue(std::locale(std::cout.getloc(), facet));
  
      cout &lt;&lt; microsec_clock::local_time() &lt;&lt; " before timer construction" &lt;&lt; endl;
      Mytimer timer;
      cout &lt;&lt; microsec_clock::local_time() &lt;&lt; " before startTimer()" &lt;&lt; endl;
      timer.startTimer();
      cout &lt;&lt; microsec_clock::local_time() &lt;&lt; " IsRunning: " &lt;&lt; timer.isRunning() &lt;&lt; endl;
      for (int i = 0; i &lt;= 20; i++)
      {
          sleep(1);
          cout &lt;&lt; microsec_clock::local_time() &lt;&lt; " IsRunning: " &lt;&lt; timer.isRunning() &lt;&lt; endl;
      }
  } 
  </code></pre>
  
  <blockquote>
    <p>20120412 22:41:45689235 before timer construction<br>
    20120412 22:41:45689514 before startTimer()<br>
    20120412 22:41:45689619 IsRunning: 1<br>
    20120412 22:41:45689693 Handler1: Timer 1 was cancelled or retriggered.<br>
    20120412 22:41:46689792 IsRunning: 1<br>
    20120412 22:41:47689994 IsRunning: 1<br>
    20120412 22:41:48690179 IsRunning: 1<br>
    20120412 22:41:49690375 IsRunning: 1<br>
    20120412 22:41:50690530 IsRunning: 1<br>
    20120412 22:41:51690712 IsRunning: 1<br>
    20120412 22:41:52690884 IsRunning: 1<br>
    20120412 22:41:53691069 IsRunning: 1<br>
    20120412 22:41:54691236 IsRunning: 0<br>
    20120412 22:41:55691428 IsRunning: 0<br>
    20120412 22:41:56691614 IsRunning: 0<br>
    20120412 22:41:57691810 IsRunning: 0<br>
    20120412 22:41:58692001 IsRunning: 0<br>
    20120412 22:41:59692193 IsRunning: 0<br>
    20120412 22:42:00692364 IsRunning: 0<br>
    20120412 22:42:01692542 IsRunning: 0<br>
    20120412 22:42:02692706 IsRunning: 0<br>
    20120412 22:42:03692886 IsRunning: 0<br>
    20120412 22:42:04693071 IsRunning: 0<br>
    20120412 22:42:05693267 IsRunning: 0<br>
    20120412 22:42:06693465 IsRunning: 0</p>
  </blockquote>