C++ boost asiodeadline_timer

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3539583/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 13:04:43  来源:igfitidea点击:

boost asio deadline_timer

c++boostboost-asio

提问by shaz

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?

我希望下面的代码打印 Hello, world! 每 5 秒一次,但发生的情况是程序暂停 5 秒,然后一遍又一遍地打印消息,没有后续的暂停。我错过了什么?

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

回答by interjay

You're creating the deadline_timeras 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.

您正在创建deadline_timer一个局部变量,然后立即退出该函数。这会导致计时器自行销毁和取消,并使用您忽略的错误代码调用您的函数,从而导致无限循环。

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

使用存储在成员或全局变量中的单个计时器对象应该可以解决此问题。

回答by Bill Lynch

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

如果您查看错误代码,则会收到操作取消错误。

回答by firstlight

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