函数deadline_timer::async_wait()的参数

The argument of function deadline_timer::async_wait()

本文关键字:参数 async deadline timer 函数 wait      更新时间:2023-10-16

我正在学习Boost.Asio,但我有一个关于Boost::Asio::deadline_timer async_wai的问题。这是来自boost主页的代码:

//
// timer.cpp
// ~~~~~~~~~
//
// Copyright (c) 2003-2013 Christopher M. Kohlhoff (chris at kohlhoff dot com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
public:
  printer(boost::asio::io_service& io)
    : timer_(io, boost::posix_time::seconds(1)),
      count_(0)
  {
    timer_.async_wait(boost::bind(&printer::print, this));
  }
  ~printer()
  {
    std::cout << "Final count is " << count_ << "n";
  }
  void print()
  {
    if (count_ < 5)
    {
      std::cout << count_ << "n";
      ++count_;
      timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
      timer_.async_wait(boost::bind(&printer::print, this));
    }
  }
private:
  boost::asio::deadline_timer timer_;
  int count_;
};
int main()
{
  boost::asio::io_service io;
  printer p(io);
  io.run();
  return 0;
}

async_wait需要这样的函数签名:

void handler(
  const boost::system::error_code& error // Result of operation.
);

但在这个圆顶中,它是timer_.async_wait(boost::bind(&printer::print, this));,签名是void print(printer*),它是如何工作的
请帮帮我,谢谢。

timer3示例中的文本"在本例中,boost::bind()的boost:,必须仅指定与处理程序的参数列表匹配的参数。在Timer.4教程中,您将看到,如果回调处理程序不需要参数,则可能会忽略此占位符。"

您不能使用boost::asio::占位符::error或在timer4示例中使用它。

示例3没有boost::asio::占位符::error

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
void print( boost::asio::deadline_timer* t, int* count)
{
    if (*count < 5)
    {
        std::cout << *count << "n";
        ++(*count);
        t->expires_at(t->expires_at() + boost::posix_time::seconds(1));
        t->async_wait(boost::bind(print, t, count));
    }
}
int main()
{
    boost::asio::io_service io;
    int count = 0;
    boost::asio::deadline_timer t(io, boost::posix_time::seconds(1));
    t.async_wait(boost::bind(print, &t, &count));
    io.run();
    std::cout << "Final count is " << count << "n";
    return 0;
}

示例4带有boost::asio::占位符::error

#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
class printer
{
    public:
        printer(boost::asio::io_service& io)
            : timer_(io, boost::posix_time::seconds(1)),
            count_(0)
    {
        timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error));
    }
        ~printer()
        {
            std::cout << "Final count is " << count_ << "n";
        }
        void print(const boost::system::error_code &e)
        {
            if (count_ < 5)
            {
                std::cout << count_ << "n";
                ++count_;
                timer_.expires_at(timer_.expires_at() + boost::posix_time::seconds(1));
                timer_.async_wait(boost::bind(&printer::print, this, boost::asio::placeholders::error ));
            }
        }
    private:
        boost::asio::deadline_timer timer_;
        int count_;
};
int main()
{
    boost::asio::io_service io;
    printer p(io);
    io.run();
    return 0;
}

Boost.Bind会忽略额外的参数,请查看Bind documentaion。

编辑:

已经有人问了一个与你类似的问题,看看吧,有更详细的答案。