title: How to restart boost deadline timer
body: <p>I have a requirement such that my timer must be reset based on 2 conditions, whichever happens earlier.</p>
  
  <ol>
  <li>When timer expires</li>
  <li>When certain condition is met (like memory reaches certain limit)</li>
  </ol>
  
  <p>I am following these steps:</p>
  
  <pre><code>boost::asio::io_service io;
  boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
  boost::mutex mtx1;
  
  void run_io_service()
  {
      io.run();
  }
  
  void print(const boost::system::error_code&amp; /*e*/)
  {
      boost::mutex::scoped_lock lock(mtx1);
      std::cout &lt;&lt; "Hello, world!\n";
      t.expires_from_now(boost::posix_time::seconds(1));
      t.async_wait(print);
      std::cout &lt;&lt; "Print executed\n";
  }
  int main()
  {
      t.async_wait(print);
      boost::thread monitoring_thread = boost::thread(run_io_service);
      boost::this_thread::sleep( boost::posix_time::seconds(2));
      t.cancel();
      std::cout &lt;&lt; "Resetting Timer\n";
      t.async_wait(print);
      boost::this_thread::sleep( boost::posix_time::seconds(2));
      t.cancel();
      io.stop();
      monitoring_thread.join();
      return 0;
  }
  </code></pre>
  
  <p>This code works fine till the time timer hasn't been cancelled.
  <strong>Once timer has been cancelled, timer doesn't work in expected way</strong>, it doesn't work at all.</p>
  
  <p>What am I doing wrong?</p>