Skip to content

Instantly share code, notes, and snippets.

@NicolaBernini
Last active October 15, 2020 11:50
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save NicolaBernini/13c82b45ae283911eeeb to your computer and use it in GitHub Desktop.
Save NicolaBernini/13c82b45ae283911eeeb to your computer and use it in GitHub Desktop.
Boost Asio Deadline Timer - Repeating Async Wait Example
#include <iostream>
#include <boost/system/error_code.hpp>
#include <boost/asio.hpp>
#include <boost/bind.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
/**
* @brief The Timer Callback Function which reschedule the timer after 1 sec for 5 times
*/
void callback_func(const boost::system::error_code& e, boost::asio::deadline_timer* pt, int* pcont)
{
std::cout << "[Timer Callback Func] Iteration = " << *(pcont) << std::endl;
//** Checks if a new timer needs to be rescheduled
if(*(pcont) < 5)
{
//** Increments the counter
++(*pcont);
//** Updates the absolute timer target time (when the callback function will be called again)
pt->expires_at(pt->expires_at() + boost::posix_time::seconds(1));
//** Reschedule a new job for the timer
pt->async_wait(boost::bind(callback_func, boost::asio::placeholders::error, pt, pcont));
}
}
int main()
{
/**
* @brief The Counter
* @note It lives in the main() scope and its pointer is passed to the callback function (different thread) for adding up, however it is safe as the main thread (i.e. the scope of this variable) will not terminate until there is no more job for the timer
*/
int cont=0;
/**
* @brief The Boost Asio IO_Service Engine used for Async Communications
*/
boost::asio::io_service ios;
//** Define a timer specifying the IO_Service Engine and the Target Time
boost::asio::deadline_timer t(ios, boost::posix_time::seconds(1));
//** Schedule the job
t.async_wait(boost::bind(callback_func, boost::asio::placeholders::error, &t, &cont));
//** It blocks the Main Thread Execution until there is no more job for the timer
ios.run();
}
@NicolaBernini
Copy link
Author

Note:
The boost::bind makes copies of its arguments so to avoid it

  • pass by reference using boost::ref() for standard reference, and boost::cref() for constant reference
  • pass (shared) pointers
    • in this case, passed objects are statically allocated hence they belong to the main() scope so there is no need to perform explicit deallocation (their allocation is static not dynamic) so no shared pointers but standard pointers

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment