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.
Per-Operation
4.
Executors
5.
Allocators
6.
Cancellation
7.
Frequently Asked Questions
7.1.
What is Asio?
7.2.
What is a Cancellation slot?
7.3.
What does asynchronous mean?
7.4.
What is the use of the Asio library?
8.
Conclusion
Last Updated: Mar 27, 2024

Executors, Allocators, and Cancellation in Asio

Introduction

Hey Ninjas! You all must be aware of C++ language and have used it or heard about it. C++ can also be used in networking and low-level programming with the help of a C++ library called Asio.

executor allocators and cancellation in asio

In this article, we will discuss the Executors, Allocators, and Cancellation in Asio. These are the operations that every asynchronous agent (agents not occurring at the same time) has. Let’s start by learning Asio first.

Also read, Abstract Data Types in C++

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++

Per-Operation

The Asio agent, like an I/O object or a timer, initiates an individual asynchronous operation known as per-operation. These operations can include things like network operations (such as sending data over a socket), I/O operations (such as reading from or writing to a file), or other tasks that are initiated asynchronously.

Per-Operation

ASIO provides a number of mechanisms for managing per-operation execution, including the ability to cancel an operation that is in progress or to wait for one or more operations to complete before proceeding. This allows developers to easily coordinate the execution of multiple asynchronous tasks and to ensure that their code behaves as expected in the face of varying levels of concurrency.

Now let’s move on to the topic and first learn about Executors.

Executors

An Executor helps us to know when, where, and how an agent (an object that performs some actions asynchronously) should execute. Each asynchronous agent has a connected executor. The queueing and running of the agent’s completion handlers are determined by it. It is quite important for the agent code.

The agent’s executor can be used for multiple purposes. Let’s discuss a few.

💠 The asynchronous operation uses the agent’s executor to track the work represented by them in case of outstanding operations (operations that have been initiated but have not yet been completed).

💠 The executor ensures that the execution of an agent happens on the declared execution course closest to the data source. The execution course refers to the sequence of events that occurs during the execution of a task.

💠 The executor ensures that completion handlers enter only once; else, it can cause stack overflow or accidental recursion.

💠 It is used to define the priority for an asynchronous agent and its completion handler.

💠 It guarantees that the completion handlers of an agent are not running concurrently by coordinating such agents that share common data structures.

💠 It returns the default executor of an operation to execute the completion handlers nearest to its event that activated the completion.

💠 It is used to enable dynamic thread pools to let agents make bolder scheduling decisions, like running the agents as a unit between the execution resources.

💠 The executor puts the completion handlers in a queue such that they safely update UI(User Interface) elements while running on the GUI (Graphical User Interface) application thread.

Allocators

An allocator acts as an interface that provides an agent’s asynchronous operations to acquire per-operation stable memory resources (POSMs). As the name suggests, the memory obtained is per operation as it is available only for the lifetime of an operation. Stable represents that memory will be available at the allotted location across the span of the operation. Each asynchronous agent has an associated allocator.

Let’s see how POSMs can be utilized in different ways:

👉 The asynchronous operation can use a single and fixed-size POSM for an outstanding operation. For example, a Linked list is used to store some states of the agents.

👉 The operation can use a runtime-sized POSM to store a duplicate of the user-supplied buffer like a runtime-sized array.

👉 The multiple POSMs can be used by an operation simultaneously, like a runtime-sized array for a buffer and a fixed-size POSM for a linked list.

👉 The multiple POSMs of variable size can be used sequentially, like multiple runtime-sized arrays for a buffer.

👉 Sometimes there isn’t any requirement for POSMs like memory management for an operation that can be performed by an existing API (Application Programming Interface), or a circular buffer (data structure which is connected end-to-end) can be used to store states having a long life.

Cancellation

Every asynchronous agent has an associated cancellation slot. This is used to enable the per-operation cancellation. Many objects in Asio, like timers and sockets, have support for object-wide cancellation. They can close or cancel the member functions of the outstanding asynchronous operations. Some asynchronous operations can also perform individual and targeted cancellations. Cancellation is used mostly when an operation contains multiple child agents. In that case, if one agent is not required any further, it can be cancelled.

The cancellation handler is required for an asynchronous operation to support cancellation. Here are some important points regarding the cancellation handler:

📌 The cancellation handler is installed in an agent’s cancellation slot.

📌 The cancellation handler is a function object and is associated with a single agent.

📌 When a cancellation signal is emitted by a user into the slot, it invokes the cancellation handler.

📌 The cancellation slot includes only a single handler at a time, and adding a new handler will overwrite the existing handler.

📌 The cancellation slots can be reused for the prior asynchronous operations within an agent. 


Also see, Application of Oops

Also see, Abstract Data Types in C++

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 is a Cancellation slot?

The cancellation slot is used to install the cancellation handler. Each asynchronous operation has its own associated cancellation slot. The cancellation handler is used to close or cancel the operations of an asynchronous operation.

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 Executors, allocators, and cancellation in Asio. We have various important points regarding the three. You can check out our other articles if you want to dive deep into other concepts related to Asio -

👉 Asynchronous operations and Agents

👉 Stackless and Stackful coroutine

👉 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