Sync Timer
The timer named Deadline_timer is provided in ASIO, which provides the ability to time out the timer. First, take the simplest synchronous timer as an example to demonstrate how to use it.
#include
<iostream>
#include
<boost/asio.hpp>
int main ()
{
Boost::asio::io_service
Io
Boost::asio::d Eadline_timer
Timer (IO, boost::p osix_time::seconds (3));
Timer.wait ();
Std::cout << "Hello, world!\n";
return 0;
}
First, a Io_service object is common, which provides the IO scheduling function, and all IO operations in the ASIO library are performed based on it. It then creates a Deadline_timer object that has two parameters, one for the Io_service object and the other for the time-out.
After you create a timer, you can call the wait function to block waiting until the timer expires, and it also has an overloaded form that specifies the entry parameter for the error code, which is described later in the error code.
Asynchronous timer
Synchronizing a timer is simple, but because it is blocked, it is not commonly used in actual projects, and often uses an asynchronous timer: Specifies a callback function that executes the callback function after the timer expires. The implementation of asynchronous timer in ASIO is simple, the example is as follows:
)
{
Std::cout <<;
}
Main ()
{
Boost::asio:: IO;
Boost::asio::(5));
Timer.async_wait (&print);
Io.run ();
return 0;
}
Compared to synchronous mode, it has two different points:
- Called a non-blocking function, async_wait, whose entry is a callback function.
- Explicitly call the Io_service.run () function to drive asynchronous IO scheduling.
Cancel Timer
The timer also has a common operation is to cancel the timer, the basic method is as follows:
- Call the Cancel function of the timer to cancel the timer
- After the timer is canceled, the callback function executes immediately and can be sensed by Err_code whether the timer has been canceled
void print (const boost::system::error_code& ERR)
{
if (ERR)
{
Std::cout << "timer is canceled\n";
Return
}
Std::cout << "Hello, world!\n";
}
int main ()
{
Boost::asio::io_service io;
Boost::asio::d eadline_timer timer (io, boost::p osix_time::seconds (5));
Timer.async_wait (&print);
Boost::asio::d eadline_timer timer2 (io, boost::p osix_time::seconds (2));
Timer2.wait ();
Timer.cancel ();
Io.run ();
return 0;
}
Changing the timer time-out
You can change the timer's time-out by using the Expires_from_now and expires_at two functions, and the following example implements a cycle timer.
typedef std::function<void (const boost::system::error_code&) > Timer_callback;
void print (const boost::system::error_code&)
{
Std::cout << "Hello, world!\n";
}
int main ()
{
Boost::asio::io_service io;
Boost::asio::d eadline_timer timer (io, boost::p osix_time::seconds (1));
Timer_callback callback = [&] (const boost::system::error_code& ERR)
{
print (ERR);
Timer.expires_at (Timer.expires_at () + boost::p osix_time::seconds (1));
Timer.async_wait (callback);
};
Timer.async_wait (callback);
Io.run ();
return 0;
}
PS: For the sake of simplicity, the c++11 syntax is used here, and you do not want to use c++11 syntax to refer to the original example of the boost document.
Boost.asio Series--timer