Hey Ninja! Welcome to another article on Asio. When you start learning about C++ and start programming, you may wonder how certain functions like input, output, and networking are performed using programming languages. For C++, such functions can be performed using the Asio library.
This article will discuss Asio. We will discuss signal handling in Asio, and we will also discuss the signal_set class in Asio.
C++ is an open-source, high-level, general-purpose programming language. It is a popular and widely used programming language. It supports both functional and object-oriented programming principles. It is widely used because of its speed. It is faster than most other high-level programming languages like Java and Python.
C++ is famous for its simplicity. It is a simple and straightforward yet powerful programming language generally used for developing operating systems, gaming applications, web browsers, embedded systems, compilers, and database management systems. It is one of the oldest languages and an extension of the C programming language.
It consists of various libraries and frameworks that make the usage of C++ easier. Various libraries have specific functionalities; one such library is Asio. Let us now understand Asio.
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. It is released under Boost Software License. Multiple platforms support Asio, including Windows, Linux, macOS, and FreeBSD.
Multiple projects have used Asio, including the WebSocket framework, Osiris, CodeShop, JukeFly, and Web toolkit. There are two variants of Asio involving the Boost.Asio and non-boost Asio. Boost is another library of C++.
Signal Handling
Signal handling is one of the important topics of Asio. When we establish a connection in a network using the Asio library of C++, it is essential to handle the signals. We have to ensure that the signals are delivered.
In Asio, signal handling is done using the basic_signal_set class. The typedef signal_set is used for the basic_signal_set class.
typedef basic_signal_set signal_set;
You can create an object of the signal_set class and add the multiple signals to be delivered. The signal_set class contains various methods you will need to perform signal handling.
The important member functions of the signal set class are as follows.
basic_signal_set::basic_signal_set
It is the constructor of the signal_set class. It initializes the signal_set object. There are four constructors available in the signal_set class. They are
Constructor to initialize the signal_set object with no signals.
Constructor to initialize an object with a single signal.
Constructor to initialize an object with two signals.
Constructor to initialize an object with three signals.
If you want to add more signals, you have to use the add functionof the signal_set class, which we will discuss next.
basic_signal_set::add
This method is used to add signals to a signal_set object. It takes the signal number as the only parameter to add the specific signals to the signal_set.
basic_signal_set::async_wait
The async_wait method is used to start an asynchronous operation to wait for the set of signals to be delivered. The completion handler will be called exactly once for each async_wait() request.
The async_wait() function takes a single parameter which is a token. The token or the completion token gives the completion handler, which will be called after the wait is over.
basic_signal_set::cancel
You may also want to cancel the operations associated with the signal_set at some time. It is done in Asio using the cancel method. It does not take any parameters. The cancel method forces the completion of any ongoing asynchronous wait actions against the signal set. The collection of registered signals is not affected by the cancellation.
basic_signal_set::clear
As the name suggests, the clear method is used to clear or remove all the signals from the signal_set. If the signal_set is empty, this method has no effects and does not raise an error. This method also doesn’t take any parameters.
basic_signal_set::get_executor
This method is used to fetch the executor associated with the object.
basic_signal_set::remove
The remove method removes a specific signal from the signal_set. It does nothing if the signal is not present in the signal_set and does not raise an error. It takes only one parameter. The parameter it takes is the signal_number to specify the signal to be removed from the signal_set.
basic_signal_set::~basic_signal_set
This is the destructor of the class. It performs the opposite function of the constructor. It destroys and deletes the signal_set object. If any asynchronous wait operations are pending, they are cancelled when the destructor is invoked.
Let’s understand Asio-Signal Handling by using a few member functions of class signal_set.
Implementation
#include <iostream>
#include <asio/signal_set.hpp>
#include <asio/error_code.hpp>
using namespace std;
void handler(const error_code, int signal_number)
{
std::cout << "The handling signal is " << signal_number << std::endl;
}
int main(int argc, char** argv)
{
asio::io_context io;
asio::signal_set signal(io);
int signal_number = 10;
std::cout << "Adding Signal to a signal set using signal.add(SIGINT)" << endl;
signal.add(SIGINT);
std:cout << "Print executor " << signal.get_executor() << endl;
handler(error_code{}, signal_number);
std::cout << "Removing Signal to a signal set using signal.remove(SIGINT)" << endl;
signal.remove(SIGINT);
io.run();
}
Explanation
In the above code, we have used member functions of signal_set class. We have made a function named handler which takes two parameters error_code and signal_number. Inside this function, we are printing the “handling signal number”. Inside our main function, we have assigned any random number to the signal_number variable. We have used signal.add() and signal.remove() functions for adding and removing a signal to a signal set respectively. We have also printed the executor using signal.get_executor(). We have called the handler() function with two parameters error_code{} and signal_number.
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 ASIO stand for in C++?
Asio in C++ stands for asynchronous input-output. Asynchronous input-output is a processing model that allows other processing to continue before the transmission(whether input or output) has finished.
How does the signal_set work in Asio?
In Asio, with the help of signal_set, you can perform signal handling. You can create an object with multiple signals and perform the asynchronous operation to wait for the set of signals to be delivered.
Asio is written in which language?
Asio is a C++ library written in C++. It is as simple as C++ and supports objected oriented programming. It has various classes to perform certain functions specific to i/o and networking.
Is C++ a fast programming language?
C++ is a fast programming language compared to other high-level languages like Java and Python.
Conclusion
This article discussed the Asio library of C++. We have discussed signal handling in Asio. We discussed the single_set class and its important methods in Asio.
To learn more about C++ and programming, visit the below articles.
We hope you have gained a better understanding of these topics now! Are you planning to ace the interviews with reputed product-based companies like Amazon, Google, Microsoft, and more?