12

I expected the code below to print Hello, world! every 5 seconds, but what happens is that the program pauses for 5 seconds and then prints the message over and over with no subsequent pauses. What am I missing?

#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace boost::asio;
using namespace std;

io_service io;

void print(const boost::system::error_code& /*e*/)
{
  cout << "Hello, world!\n";
  deadline_timer t(io, boost::posix_time::seconds(5));
  t.async_wait(print);
}


int main()
{

  deadline_timer t(io, boost::posix_time::seconds(5));
  t.async_wait(print);

  io.run();

  return 0;
}

edit to add working code below. thanks guys.

#include <iostream>
#include <boost/bind.hpp>
#include <boost/thread.hpp>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace boost::asio;
using namespace std;

class Deadline {
public:
    Deadline(deadline_timer &timer) : t(timer) {
        wait();
    }

    void timeout(const boost::system::error_code &e) {
        if (e)
            return;
        cout << "tick" << endl;
        wait();
    }

    void cancel() {
        t.cancel();
    }


private:
    void wait() {
        t.expires_from_now(boost::posix_time::seconds(5));
        t.async_wait(boost::bind(&Deadline::timeout, this, boost::asio::placeholders::error));
    }

    deadline_timer &t;
};


class CancelDeadline {
public:
    CancelDeadline(Deadline &d) :dl(d) { }
    void operator()() {
        string cancel;
        cin >> cancel;
        dl.cancel();
        return;
    }
private:
    Deadline &dl;
};



int main()
{
    io_service io;
    deadline_timer t(io);
    Deadline d(t);
    CancelDeadline cd(d);
    boost::thread thr1(cd);
    io.run();
    return 0;
}
Sam Miller
  • 23,808
  • 4
  • 67
  • 87
shaz
  • 2,317
  • 4
  • 27
  • 37
  • what `CancelDeadline` is for? i've commented this class and `CancelDeadline cd(d); boost::thread thr1(cd);` and code still works. – Oleg Vazhnev May 03 '13 at 08:14
  • You may be interested in this question: http://stackoverflow.com/questions/21771639/can-i-use-a-stackful-coroutine-as-the-wait-handler-of-a-steady-timer-which-is-de – updogliu Feb 14 '14 at 06:06

3 Answers3

27

You're creating the deadline_timer as a local variable and then immediately exiting the function. This causes the timer to destruct and cancel itself, and calls your function with an error code which you ignore, causing the infinite loop.

Using a single timer object, stored in a member or global variable, should fix this.

interjay
  • 107,303
  • 21
  • 270
  • 254
4
#include <iostream>
#include <boost/asio.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

using namespace boost::asio;
using namespace std;

io_service io;

deadline_timer t(io, boost::posix_time::seconds(5));

void print(const boost::system::error_code& /*e*/)
{
  cout << "Hello, world!\n";
  t.expires_from_now(boost::posix_time::seconds(5));
  t.async_wait(print);
}


int main()
{

  //deadline_timer t(io, boost::posix_time::seconds(5));
  t.async_wait(print);

  io.run();

  return 0;
}
firstlight
  • 57
  • 1
  • 1
  • 5
    Care to add any sort of comments? Having an answer that is only code rarely actually helps the question asker. – Dennis Meng Aug 27 '13 at 00:42
2

If you look at the error code, you're getting operation cancelled errors.

Bill Lynch
  • 80,138
  • 16
  • 128
  • 173