Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
Hey Ninjas! You must have heard of a C++ library, Asio. In this article, we are going to study the asynchronous operations and agents in Asio. They are part of the networking domain designed to increase efficiency and scalability and provide network-oriented services.
We will be discussing asynchronous operations, their lifecycle, and the inheritance from synchronous operations, and the second section of this article will cover asynchronous agents and their working. So let’s start by first learning 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.
Now let’s move on to understanding asynchronous operations.
Asynchronous Operations
The Asio asynchronous model promotes asynchronous operations as the fundamental composition. They are the basic unit and represent the background work except for the user’s code which can carry on with other tasks. They support many approaches like callbacks, fibers, futures (eager and lazy), and many others. These approaches help the programmers to choose the best approach for the trade-offs.
Let’s understand the functioning of the life cycle of an asynchronous operation.
The asynchronous operations overall are divided into two phases. Let’s see both phases with the events of the operation.
Phase-1
This phase starts with the call of the initiating function to the asynchronous operation; the operation is in the outstanding stage.
Phase-2
Phase 2 begins when the queueing of the completion handler is completed for the execution. When the asynchronous operation is completed, the resultant is displayed by the completion handler.
Initiating Function and Completion Handler
We have used the terms initiating function and the completion handler in the life cycle; these two are the important functions that are present in the user’s code. Refer to the following image.
Initiating Function
The initiating function is a function that can be called by the user to invoke the asynchronous operation.
Completion Handler
A completion handler is a function object that is provided by the user, which can be invoked only once to get the output of the asynchronous operation.
Inheritance from Synchronous Operation
Many properties of asynchronous operations are inherited from the synchronous operation. These semantic properties improve flexibility and enhance the composition. Let’s look at the properties of synchronous operations and equivalent properties of asynchronous operations.
Now let’s move on to the next section, Asynchronous agents.
Asynchronous Agents
An asynchronous agent comprises asynchronous operations. These agents work collectively with other agents, and every asynchronous operation belongs to the agent. The asynchronous agents might also contain only one operation. Just like threads are for synchronous operations, here, asynchronous agents are for asynchronous operations.
Let’s understand, with a flowchart, what are the associated characteristics of asynchronous agents that are used by asynchronous operations.
The asynchronous agent does not require a concrete mechanism; it is completely based on the imaginary construct, which provides the construct and the composition of the asynchronous operation. The asynchronous agents run the completion handler after the asynchronous operation is completed. These are just the smaller units of the work that needs to be scheduled, which is done by asynchronous agents after waiting for the asynchronous operation to end. This can be better understood by this figure.
Let’s understand asynchronous operations better with the help of an example.
Implementation of Asynchronous Operation
#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);
std::cout << "This statement is printed before the function call and we know async_wait() returns immediately.\nThis confirms async_wait() is non-blocking call!\n";
io.run();
return 0;
}
You can also try this code with Online C++ Compiler
We have shown the use of the asynchronous operation async_wait() in this example. In the Asio library, the function async_wait() is used to wait till the completion of an asynchronous operation. The asynchronous operation does not block the execution of any operation if more than one operations are in progress. We have called a function that prints a statement and, after that, another print statement. This second statement will be printed before.
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 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 are the events in the life cycle of asynchronous operations?
The asynchronous operation starts with a call to the initiating function; In this phase, the operation is called outstanding. After this, the completion handler is placed in the queue for execution. The operation is completed, and the results are displayed.
How are asynchronous operations different from synchronous operations?
The operations and programs in asynchronous operations can run in parallel, while the programs in synchronous operations will run one at a time. The asynchronous operations are non-blocking, i.e., they can send more than one request to the server, while the synchronous operation is blocking and can send only one request.
Conclusion
This article was about Asio, which is a C++ library for networking and communication. We have discussed the asynchronous operation and its lifecycle. We have also learned about asynchronous agents and it’s working mechanisms. You can check out our other articles if you want to dive deep into other concepts related to Asio -