【问题标题】:The argument of function deadline_timer::async_wait()函数deadline_timer::async_wait()的参数
【发布时间】:2013-11-29 04:47:35
【问题描述】:

我正在学习 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(&amp;printer::print, this));,签名是void print(printer*),它是如何工作的?
请帮帮我,谢谢。

【问题讨论】:

  • 我会使用std::bind 并尽量少使用boost 的东西(我在我的项目中使用boost.asio 并且只包括boost/asio.hpp)。请参阅stackoverflow.com/a/17414563/2176127 了解如何使用std::bind 而不是boost::bind
  • See this article 以更好地了解 `bind 的工作原理。
  • @IgorR。很棒的文章!

标签: c++ boost


【解决方案1】:

来自timer3 example的短信 “在此示例中, boost::bind() 的 boost::asio::placeholders::error 参数是传递给处理程序的错误对象的命名占位符。启动异步操作时,如果使用 boost::bind (),您必须仅指定与处理程序的参数列表匹配的参数。在教程 Timer.4 中,您将看到如果回调处理程序不需要该参数,则此占位符可能会被省略。"

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

没有 boost::asio::placeholders::error 的示例 3

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

带有 boost::asio::placeholders::error 的示例 4

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

【讨论】:

    【解决方案2】:

    Boost.Bind 会默默忽略额外的参数,请查看 bind 文档。

    编辑:

    已经有人问过和你类似的question,看看吧,有更详细的答案。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-04-08
      • 2019-02-23
      • 2014-11-15
      • 1970-01-01
      • 1970-01-01
      • 2019-02-02
      • 1970-01-01
      相关资源
      最近更新 更多