timer.3-binding arguments to a handler

Source: Internet
Author: User

In this tutorial we'll modify the program from tutorial Timer.2 So, the Timer fires once a second. This would show how to pass the additional parameters to your handler function.

#include<iostream> #include <boost /asio. Hpp> #include < boost/bind.hpp> #include << span class= "identifier" >boost/date_time/< span class= "identifier" >posix_time/posix_time hpp>           

To implement a repeating timer using ASIO you need to change the timer's expiry time in your callback function, and to the N Start a new asynchronous wait. Obviously this means, the callback function would need to being able to access the timer object. To this end we add a new parameters to the print function:

    • A pointer to a timer object.
    • A counter So, we can stop the program when the timer fires for the sixth time.
void print ( span class= "keyword" >const boost::system ::error_code& /* E*/boost::asio< Span class= "Special" >::deadline_timer* t int* count{          

As mentioned above, this tutorial program uses a counter-stop running when the timer is fires for the sixth time. However you'll observe that there are no explicit call to ask the Io_service to stop. Recall The tutorial Timer.2 we learnt that the Boost::asio::io_service::run () function completes when there was no more ' Work ' to do. By does starting a new asynchronous wait on the timer when count  reaches 5, T He io_service'll run out of the work and stop running.

  (*5std::*"\ n"+ + (*count);       

Next we move the expiry time for the timer along by one second from the previous expiry time. By calculating the new expiry time relative to the old, we can ensure that the timer does isn't drift away from the Whole-se Cond Mark due to any delays in processing the handler.

 t-> Expires_at (t-> expires_at () +  Boost::posix_time:: seconds (1  

Then we start a new asynchronous wait on the timer. As can see, the Boost::bind () function was used to associate the extra parameters with your callback handler. The Boost::asio::d eadline_timer::async_wait () function expects a handler function (or function object) with the signature void(const boost::system::error_code&). Binding The additional parameters converts your print function into a function object that matches the signature correctly .

See the Boost.bind documentation for more information on how to use Boost::bind ().

In this example, the Boost::asio::p laceholders::error argument to Boost::bind () are a named placeholder for the error OBJEC T passed to the handler. When initiating the asynchronous operation, and if using Boost::bind (), you must specify only the arguments that match the Handler ' s parameter list. In tutorial timer.4 you'll see that this placeholder could be elided if the parameter are not needed by the callback handle R.

    T-Async_wait(Boost::bind (print boost::asio::placeholders::error span class= "identifier" >tcount}}int main  () {boost::asio::io_service io< Span class= "Special",               

A new variable is added so, we can stop the program time, the count timer fires for the sixth time.

  0;  boost::ASIO::t(ioboost::posix_time::seconds( 1));               

As in Step 4, when making the Boost::asio::d eadline_timer::async_wait () from main  we Bind the additional parameters needed for The print  function.

 t. Async_wait (boost: bind (print boost::asio::placeholders::error Span class= "Special" >&t&countio. Run             

Finally, just to prove count the variable is being used print in the handler function, we'll print out its new Val Ue.

  STD::"\ n"0;}    

See the full source listing

Return to the tutorial index

Previous:timer.2-using a Timer asynchronously

Next:timer.4-using a member function as a handler

timer.3-binding arguments to a handler

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.