Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
What is Per-Operation Cancellation?
3.
What is a Cancellation Slot?
4.
Cancellation Slot Type Requirements
4.1.
Implementation
4.2.
Explanation
4.3.
Output 
5.
Frequently Asked Questions
5.1.
What is the full form of Asio?
5.2.
What is the use of the Asio library?
5.3.
What is a cancellation slot?
5.4.
How can we enable per-operation cancellation?
6.
Conclusion
Last Updated: Mar 27, 2024
Medium

Introduction to Per-Operation Cancellation

Introduction

Do you know that C++ library Asio supports Per-Operation Cancellation? Do you know how Per-Operation Cancellation is enabled? Do you know how cancellation slot type requirements are satisfied?

Introduction to Per-Operation Cancellation

In this article, we will discuss about Per-Operation Cancellation. We will also discuss the cancellation slot. We will make one program to better understand the Per-Operation Cancellation. Moving forward, let’s understand about Per-Operation Cancellation.

Also read, Abstract Data Types in C++

What is Per-Operation Cancellation?

The input-output objects support the cancellation of asynchronous operations using close() and cancel() methods. There are specific asynchronous operations that support separate and selective cancellation and can be enabled by specifying cancellation slots. So, we can say that per-operation cancellation is enabled by specifying the cancellation slot of the completion handler that must satisfy the cancellation slot requirements. The completion handler is the function object that is called at most once and gives the result of the asynchronous operation. Moving forward, let’s understand the cancellation slot.

What is a Cancellation Slot?

A cancellation slot can be defined as lightweight channels that are used to deliver cancellation requests. In an asynchronous operation, we can get the cancellation slot using the get_associated_cancellation_slot method. The associated cancellation slot has to satisfy the cancellation slot type requirements. Moving forward, let’s understand cancellation slot type requirements.

Cancellation Slots

Cancellation Slot Type Requirements

There are cancellation slot type requirements that must be fulfilled in slot cancellation. Let’s suppose x and y are two values.

  • If x == y, then true will be returned, and our program will not exit with an exception.
  • If x != y, then false will be returned, and our program will exit with an exception.

 

There are different cancellation slots like associated_cancellation_slot, bind_cancellation_slot, and cancellation_signal that can be used by including headers asio/associated_cancellation_slot.hpp, asio/bind_cancellation_slot.hpp, asio/cancellation_signal.hpp respectively.

Let’s make a program to better understand Per-Operation Cancellation.

Implementation

#include <iostream>

#include <asio/experimental/channel.hpp>

#include <asio/experimental/awaitable_operators.hpp>

#include <asio.hpp>

#include <asio/error_code.hpp>


using namespace std;

using namespace asio::experimental::awaitable_operators;

using channel = asio::experimental::channel< void(error_code, uint64_t) >;


asio::awaitable<void> ninja(channel& ch,int expectedNumber) {

    std::cout << "The expected number is " << expectedNumber << std::endl;

    int n = 5;

    std::cout << "The number we are checking with is equal to " << n << std::endl;

    if (n == expectedNumber) {

        std::cout << "No Error" << std::endl;

    }

    assert(n == expectedNumber && "The variable n and expected number is not same");

    co_return ;

}


int main() {

    auto io = asio::io_context{};

    auto ch = channel{ io,0};

    asio::co_spawn(io, ninja(ch, 15), asio::detached);

    io.run();

    return 0;

}

Explanation

In the above code, we have made a cancellation slot named ninja. In the main function co_spawn function is called with three arguments that are context, ninja cancellation slot, and a completion token detached. The completion token helps in producing a completion handler. Here, we are using detached() as a completion token which is used to indicate the detached asynchronous operations. We are passing and expected number 15 to the ninja cancellation slot, and inside the ninja cancellation slot, we are declaring and assigning a variable n = 5 and checking whether n and expectedNumber are equal or not; if it’s equal, then the program will print No Error in the console otherwise we are asserting a debug error message with three options Abort, Retry and Ignore. 

Clicking on Abort and Ignore will exit the code, whereas if we click on the Retry button, it will take us to the breaking point in our code. See the below images for your reference.

Output 

Output Screen

After clicking on the Retry button, we will be redirected to the breakpoint.

Redirected to Breakpoint

Also see, Application of Oops

Frequently Asked Questions

What is the full form of Asio?

Asio stands for Asynchronous Input Output.

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.

What is a cancellation slot?

A cancellation slot can be defined as a lightweight channel that is used to deliver cancellation requests.

How can we enable per-operation cancellation?

The per-operation cancellation is enabled by specifying the cancellation slot of the completion handler that must satisfy the cancellation slot requirements.

Conclusion

In this article, we have discussed about per-operation cancellation. We have also discussed about the cancellation slot. We have seen cancellation slot-type requirements. In the end, we have made one program to better understand per-operation cancellation. To learn more about the Asio library, you can read the below-mentioned articles:

⭐ Getting started with Asio

⭐ Asio-Serial Ports

⭐ Asio-Timers

⭐ Asio-Files

⭐ Asio-Channels


We hope this article has helped you in understanding a bit more about Asio library. You can read more such articles on our platform, Coding Ninjas Studio. You will find articles on almost every topic on our platform. Also, for cracking good product-based companies, you can practice coding questions at Coding Ninjas. For interview preparations, you can read the Interview Experiences of popular companies. 

Happy Coding!

Live masterclass