Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Asio?
3.
Asio Timers
3.1.
Using a timer Synchronously
3.1.1.
Example
3.1.2.
Explanation
3.1.3.
Output
3.2.
Using a Timer Asynchronously
3.2.1.
Example
3.2.2.
Explanation
3.2.3.
Output
3.3.
basic_waitable_timer
3.3.1.
Syntax
3.3.2.
Types
3.4.
steady_timer
3.4.1.
Syntax
3.4.2.
Requirements
3.5.
system_timer
3.5.1.
Syntax
3.5.2.
Requirements
3.6.
high_resolution_timer
3.6.1.
Syntax
3.6.2.
Requirements
4.
Frequently Asked Questions
4.1.
What is Asio?
4.2.
What are the Asio Timers?
4.3.
What does asynchronous mean?
4.4.
What is the use of the Asio library?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Asio Timers

Introduction

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.

asio timers

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.

C++

Now let’s move on to the topic of Asio Timer.

Also read about, Abstract Data Types in C++ and Application of Oops

Asio Timers

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.

Example

#include <iostream>
#include <asio.hpp>

using namespace std;

int main()
{
    asio::io_context io;
    asio::steady_timer t(io, asio::chrono::seconds(5));
    t.wait();
    cout << "Hello, Ninjas!!" << std::endl;

    return 0;
}

Explanation

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

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.

Example

#include <iostream>
#include <asio.hpp>

using namespace std;

void print(const asio::error_code& /*e*/)
{
    cout << "Hello, Ninjas!!" << std::endl;
}

int main()
{
    asio::io_context io;

    asio::steady_timer t(io, asio::chrono::seconds(5));

    t.async_wait(&print);

    io.run();

    return 0;
}

Explanation

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

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.

Try this code with online C++ Compiler for better understanding.

basic_waitable_timer

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

Types of basic_waitable timer

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.

Syntax

typedef basic_waitable_timer< chrono::steady_clock > steady_timer;

Requirements

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.

Syntax

typedef basic_waitable_timer< chrono::system_clock > system_timer;

Requirements

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.

Syntax

typedef basic_waitable_timer< chrono::high_resolution_clock > high_resolution_timer;

Requirements

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 -

👉 Asio Serial Ports

👉 Asio Files

👉 TCP, UDP, and ICMP Networking

 

You can refer to our guided paths on the Coding Ninjas Studio platform to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. To practice and improve yourself in the interview, you can also check out Top 100 SQL problemsInterview experienceCoding interview questionsand the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow.

Happy Coding !!

Live masterclass