Hey Ninjas! Welcome to one more tutorial on Asio. You all must be aware of C++ language and have used it or heard about it. Do you know C++ can also be used in networking and low-level programming? This is done with a library in C++ named Asio.
In this article, we will discuss the Asio Timers, synchronous and asynchronous operations, waitable timer functionality, timers based on the steady clock, etc. So let’s begin by first understanding Asio.
What is Asio?
Asio, asynchronous input-output, is an open-source, cross-platform C++ library. It is designed for input, output, and networking. It was developed in 2003 by Christopher M. Kohlhoff and was released under Boost Software License. It provides an asynchronous model to the developers using a modern approach of C++. It is supported by a huge number of platforms like Windows, macOS, Linux, etc.
A timer is used to control the event’s sequence while counting the fixed intervals. It is a clock and can be repeated or initiated for a certain period of time. In Asio, Long running I/O operations are provided with a deadline by which they should be finished. It can be shown in the form of absolute times, but usually, it is calculated proportionally to the current time.
There are two ways by which we can use the timer, synchronously and asynchronously.
Using a timer Synchronously
Synchronous or blocking architecture is used for reactive systems. It follows a definite set of sequences through which one operation can be performed at one time. Let’s see how we can use the Asio timer synchronously.
The programs that are using Asio should have at least one context of I/O execution, like the io_context object. It provides access to the functionality of I/O. The object of asio::steady_timer is declared with the executor as the first argument and a second argument to set a timer of 5 seconds expiry.
This example is used to demonstrate the simple blocking wait on the timer. If steady_timer::wait() is called on the expired timer, the execution will end, and we print “Hello, Ninjas!!” to show that the timer expires.
Output
Using a Timer Asynchronously
The asynchronous approach is a multithread-based model for communication and networking. It does not block the execution of any operation if more than one operations are in progress. The following example discusses the asynchronous approach of the Asio Timer.
This example illustrates the asynchronous wait on the timer. Instead of blocking wait as in synchronous, we call the steady_timer::async_wait() method for the asynchronous wait. Last, we call the io_context::run() method. It is also known as the completing handler, which will never invoke the asynchronous wait.
Output
These were the Asio timer synchronous and asynchronous operations; now, we will discuss the class used for Providing waitable timer functionality and its functions.
The class used for the functionality of the waitable timer is the basic_waitable_timer class. It has the ability to perform an asynchronous or blocking wait till the timer expires. It is always in either an expired or not expired state. The wait operation will be finished instantly if the wait() or async_wait() method is called on an expired timer.
Syntax
template <
typename Clock,
typename WaitTraits,
typename Executor>
class basic_waitable_timer
Types
steady_timer
The steady clock has a timer based on it. The typedef for this timer is steady_timer. This functionality can be used with the Boost Chrono library or the C++ 11 standard library’s <chrono> feature.
The header file asio/steady_timer.hpp and convenience header asio.hpp are required.
system_timer
The system clock synchronizes the computer’s operations at the correct time. The system_timer is the typedef for the timer, which is based on the system clock.
The header file asio/system_timer.hpp and convenience header asio.hpp are required for this.
high_resolution_timer
The high-resolution clock is used for hardware and is used to continuously update frequency or count. The typedef for this clock is high_resolution_timer.
The header file asio/high_resolution_timer.hpp and convenience header asio.hpp are required for this.
Frequently Asked Questions
What is Asio?
Asio is a cross-platform, open-source C++ library. It is an underrated C++ library designed for input, output, and networking purposes. Asio stands for asynchronous input-output. Christopher M. Kohlhoff developed Asio in 2003.
What are the Asio Timers?
The Asio Timers are used to control the sequence of events while calculating the fixed time intervals. There are deadlines for long-running I/O operations, which are expressed by Asio Timers.
What does asynchronous mean?
Asynchronous means that a process is independent of the execution of any other process. The execution is not blocked for operations if more than one is in progress.
What is the use of the Asio library?
Asio helps in doing network programming using C++ language. It allows the processing of data asynchronously by providing asynchronous I/O models.
Conclusion
This article was about Asio, which is a C++ library for networking and communication. We have learned about Asio Timers for synchronous and asynchronous operations, the basic_waitable_timer class, and typedef for steady, system, and high-resolution clocks. You can check out our other articles if you want to dive deep into other concepts related to Asio -