2013-11-29 46 views
1

我在學習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*),它是如何工作的?
請幫助我,謝謝。

+0

我最好使用'STD: :bind'並嘗試使用盡可能少的'boost'東西(我在我的項目中使用'boost.asio',並且只包含'boost/asio.hpp')。請參閱http://stackoverflow.com/a/17414563/2176127以瞭解如何使用'std :: bind'而不是'boost :: bind'。 – user2176127

+1

[看這篇文章](http://blog.think-async.com/2010/04/bind-illustrated.html),以更好地瞭解如何綁定工作。 –

+0

@IgorR。好文章! – user2176127

回答

1

來自timer3 example的文本 「在本例中,boost :: bind()的boost :: asio :: placeholders :: error參數是傳遞給處理程序的錯誤對象的命名佔位符。 ,如果使用boost :: bind(),則只能指定匹配處理程序參數列表的參數。在Timer.4教程中,如果回調處理程序不需要該參數,則可以看到該佔位符可能會被忽略。

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

實施例3無升壓:: ASIO ::佔位符::錯誤

#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與升壓:: ASIO ::佔位符::錯誤

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

非常感謝。 – hahaya

0

Boost.Bind靜靜地忽略額外的參數,看看bind文檔。

編輯:

類似question到你已經問,看看吧,還有更詳細的解答。

+0

好的,謝謝。 – hahaya