简体   繁体   English

提升asio deadline_timer

[英]boost asio deadline_timer

I expected the code below to print Hello, world! 我期待下面的代码打印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. 每5秒钟,但会发生的是程序暂停5秒钟,然后一遍又一遍地打印消息而没有后续暂停。 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;
}

You're creating the deadline_timer as a local variable and then immediately exiting the function. 您将deadline_timer创建为局部变量,然后立即退出该函数。 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. 使用存储在成员或全局变量中的单个计时器对象应该解决此问题。

#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;
}

如果您查看错误代码,您将获得操作取消错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM