Header in C++20

This <stop_token> header is introduced in C++20. It provides efficient mechanisms for cancelling asynchronous operations. It gives more straightforward and different approaches for cancellation techniques using exceptions. This header file is a part of the thread support library.

What is a header?

A Header is a set of instructions that specify the structure and functionality of a particular module or data. It contains declarations for variables, functions, classes, etc. They are used to prevent errors due to incomplete definitions.

What are asynchronous operations?

Asynchronous operations allow the computer program to perform tasks without waiting for each one to finish before moving on to the next. It is useful for handling multiple tasks simultaneously. For example, sending a message to your friend. Instead of waiting for a response before doing anything else, you can continue using your phone, like playing games or browsing. At the same time, the message is being sent and waiting for a replay in the background.

Classes present in the stop_token header:

It contains two primary classes, the stop_source and the stop_token class.

  • "stop_source":

This class represents the source of a cancellation request. It provides methods to request cancellation and check if it has been requested.

  • "stop_token":

This class represents a "view" into a stop_source. It allows checking if a cancellation has been requested. It checks if there is a need to issue a cancellation.

What is "stop_source"?

It is used to create a source of stopping tokens. It works in conjunction with stop_token. It is used to manage the lifecycle of an asynchronous operation.

Syntax of the stop_source class:

It has the following syntax:

Here, an object of the class stop_source is created and named as myStopSource. The get_token() function generates a stop_token named mystopToken(). This stop token is used to check for and stop requests.

Example:

Let us take a program to illustrate the stop_source class in C++.

Output:

<stop_token> Header in C++20

Explanation:

In this program, we are trying to control the toy car by moving forward and stopping it after moving for some time. We can stop toy cars when we want. The moveToyCar function will represent the action of the toy car moving. We create an object of stop_source named myStopSource to serve as a controller for stopping tokens. After that, we generate a corresponding stopping token, "st", using myStopSource.get_token(). Next, a jthread named toyCarThread is created, and it executes the moveToyCar function with the stopping token after letting the toy car move for 5 seconds. We request the toy car to stop by calling myStopSource.request_stop(). The program waits for the toy car thread to finish using toyCarThread.join().

What is "stop_token"?

The stop_token class is typically used in conjunction with jthread or other asynchronous operations to enable the detection of cancellation requests.

Syntax for the stop_token class:

It has the following syntax:

This syntax will contain the objects, variables and functions:

Objects:

  • myThread of type jthread represents the thread of execution.
  • Stoken of type stop_token represents the stopping tokens.

Functions:

  • The asyncOperation() function represents the asynchronous operation performed in the separate thread.
  • The request_stop() function is used to request the stop thread.
  • The join() function is used to wait for the associated thread.

Example:

Let us take a program to illustrate the stop_token class in C++:

Output:

<stop_token> Header in C++20

Explanation:

This program represents a night lamp that is ON until we decide to turn it OFF. The "nightlamp" function runs a separate thread "jthread" and checks the stop token to see if it should be turn off. The main thread waits for 5 seconds and then requests the night lamp to turn OFF by using request_stop() on the jthread.

Conclusion:

In conclusion, the <stop_token> header in C++20 introduces efficient mechanisms for cancelling asynchronous operations, providing more straightforward approaches for cancellation. The header includes essential classes such as stop_source and stop_token. These classes facilitate cooperative cancellation, allowing programs to respond to stop requests and enhancing the robustness and control of asynchronous tasks.






Latest Courses