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?
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.
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 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 tokenhelps 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
After clicking on the Retry button, we will be redirected to the breakpoint.
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:
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
Learn Advanced Excel: Ace Data Analytics Interview at Amazon
by Megna Roy, Data Analyst @ Amazon
04 Oct, 2024
01:30 PM
Ace Flipkart's Data Analytics interview by decoding its data strategy
by Ashwin Goyal, Product @ HRS Group, Ex - Udaan, OYO
29 Sep, 2024
06:30 AM
Stay irreplaceable: Master these essential AI tools for developers
by Pranav Malik, SDE 3 @ Oracle, Ex- Microsoft
03 Oct, 2024
01:30 PM
Learn Advanced Excel: Ace Data Analytics Interview at Amazon
by Megna Roy, Data Analyst @ Amazon
04 Oct, 2024
01:30 PM
Ace Flipkart's Data Analytics interview by decoding its data strategy
by Ashwin Goyal, Product @ HRS Group, Ex - Udaan, OYO