我正在学习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*)
,它是如何工作的?
请帮帮我,谢谢。
答案 0 :(得分:1)
来自timer3 example的文字 “在这个例子中,boost :: bind()的boost :: asio :: placeholders :: error参数是传递给处理程序的错误对象的命名占位符。启动异步操作时,如果使用boost :: bind (),您必须只指定与处理程序的参数列表匹配的参数。在教程Timer.4中,如果回调处理程序不需要该参数,您将看到该占位符可能被省略。“
你不能使用boost :: asio :: placeholders :: error或在timer4示例中使用它。
示例3没有boost :: asio :: placeholders :: 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 with boost :: asio :: placeholders :: 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;
}
答案 1 :(得分:0)