"C++ boost::asio Recursive timer callback" 会累积调用堆栈吗?

Do "C++ boost::asio Recursive timer callback" accumulate callstack?

我想制作一个线程以1秒为间隔定期发送网络消息的C++程序。

我听说 boost 库是支持 c++ 的最有用的跨平台库。

下面是我的第一个想法。

  1. 定义一个具有发送N/W消息的逻辑的函数¹。
  2. 使用上述函数注册定时器绑定。
  3. 1.Function 具有在块末尾注册自身的逻辑(与 2. 相同)。
  4. 然后,当此线程处于 运行ning 时,发送 N/W 消息¹ 逻辑会在之后每 1 秒递归调用一次。

基本测试完全成功。 但是,我想知道这种方式是否可能产生无限调用堆栈?(例如 timer_steadyTimerReculsive()->print2()->print2()->print2() ->print2()->print2() ...)

理论上我知道callstack是在cpu寄存器上累积的。因此,有时 NodeJS 会出现致命错误,因为来自无限回调的未执行无限调用堆栈。

如果这种方式使无限调用堆栈,我该如何解决这个程序目标的问题?

或者,告诉我如何在 Visual Studio.

中调试这个异步回调方法将不胜感激

我尝试在 Visual Studio 中 运行 调试模式。但是VS不能follow/catch处理程序绑定到io_service后的回调方法调用栈。

我的代码如下。

    void print2(const boost::system::error_code& e, boost::asio::steady_timer* timer, int* count) {
        /* N/W message sending logic here*/
        (*count)++;
        timer->expires_from_now(chrono::seconds(1));
        timer->async_wait(boost::bind(print2, boost::asio::placeholders::error, timer, count));

    }
    void timer_steadyTimerReculsive() {

        boost::asio::io_service io;
        int count = 0;
        boost::asio::steady_timer timer(io);
        timer.async_wait(boost::bind(print2, boost::asio::placeholders::error, &timer, &count));

        io.run();
    }
    int main() {
        timer_steadyTimerReculsive();

        cout << "end method" << endl;
        return 0;
    }

¹(N/W 消息逻辑是公司私有的。)

不,async_* 方法总是在将工作发布到异步执行服务后立即return。

事实上,如果你不打电话 io_service::{run|poll}[_one]() 什么都做不成。

除非您同步嵌套递归调用,否则您无需担心堆栈溢出。通过异步调用,您实际上并没有嵌套。