Synchronize Timer
The timer name provided in asio is deadline_timer, which provides the time-out function. First, we will use the simplest Timer synchronization method as an example to demonstrate how to use it.
# Include
<Iostream>
# Include
<Boost/asio. hpp>
Int main ()
{
Boost: asio: io_service
Io;
Boost: asio: deadline_timer
Timer (io, boost: posix_time: seconds (3 ));
Timer. wait ();
Std: cout <"Hello, world! \ N ";
Return 0;
}
First, an io_service object is common. It provides the IO scheduling function. All io operations in the asio library are executed based on it. Then, a deadline_timer object is created, which has two parameters: io_service object and timeout time.
After the timer is created, you can call the wait function to block the wait time to timer timeout. It also has an overload mode that can specify the input parameters of the error code, which will be described later.
Asynchronous Timer
Although it is simple to synchronize timer, It is not commonly used in actual projects because it will be blocked. It usually uses asynchronous timer: specify a callback function, and execute the callback function after the timer times out. Asio Implementation of asynchronous timer is relatively simple, for example:
Void print (const boost: system: error_code &/* e */)
{
Std: cout <"Hello, world! \ N ";
}
Int main ()
{
Boost: asio: io_service io;
Boost: asio: deadline_timer timer (io, boost: posix_time: seconds (5 ));
Timer. async_wait (& print );
Io. run ();
Return 0;
}
Compared with the synchronization method, it has two major differences:
- The non-blocking function async_wait is called. Its input parameter is a callback function.
- The io_service.run () function is explicitly called to drive asynchronous IO scheduling.
Cancel Timer
Another common operation of Timer is to cancel Timer. The basic method is as follows:
- Call the cancel function of timer to cancel timer.
- After timer is canceled, the callback function is executed immediately. The err_code can detect 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: deadline_timer timer (io, boost: posix_time: seconds (5 ));
Timer. async_wait (& print );
Boost: asio: deadline_timer timer2 (io, boost: posix_time: seconds (2 ));
Timer2.wait ();
Timer. cancel ();
Io. run ();
Return 0;
}
Change Timer timeout time
You can use the expires_from_now and expires_at functions to change the Timer timeout time. In the following example, you can use it to implement a periodic 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: deadline_timer timer (io, boost: posix_time: seconds (1 ));
Timer_callback callback = [&] (const boost: system: error_code & err)
{
Print (err );
Timer. expires_at (timer. expires_at () + boost: posix_time: seconds (1 ));
Timer. async_wait (callback );
};
Timer. async_wait (callback );
Io. run ();
Return 0;
}
PS: For simplicity, the c ++ 11 syntax is used here. If you do not want to use the c ++ 11 syntax, refer to the original example in the boost document.