Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost asio deadline_timer

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;
}
like image 328
shaz Avatar asked Aug 22 '10 00:08

shaz


3 Answers

#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;
}
like image 139
firstlight Avatar answered Nov 11 '22 20:11

firstlight


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.

like image 27
interjay Avatar answered Nov 11 '22 21:11

interjay


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

like image 2
Bill Lynch Avatar answered Nov 11 '22 21:11

Bill Lynch