Boost. asio series -- Timer

Source: Internet
Author: User

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:

  1. The non-blocking function async_wait is called. Its input parameter is a callback function.
  2. 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:

  1. Call the cancel function of timer to cancel timer.
  2. 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.

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.