我在學習Boost.Asio,但我有一個關於boost :: asio :: deadline_timer async_wai的問題。下面是從升壓主頁代碼:函數的參數deadline_timer :: async_wait()
//
// 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*)
,它是如何工作的?
請幫助我,謝謝。
我最好使用'STD: :bind'並嘗試使用盡可能少的'boost'東西(我在我的項目中使用'boost.asio',並且只包含'boost/asio.hpp')。請參閱http://stackoverflow.com/a/17414563/2176127以瞭解如何使用'std :: bind'而不是'boost :: bind'。 – user2176127
[看這篇文章](http://blog.think-async.com/2010/04/bind-illustrated.html),以更好地瞭解如何綁定工作。 –
@IgorR。好文章! – user2176127