Get the App
SLTechnology News&Howtos  ›  Development  › 

How to understand the classes and related functions in the lt;future> header file in the Category 11 standard

Shulou Source: shulou.com Published: 2022-06-03 05:05:28 10月01日 Update

This article introduces you how to understand the classes and related functions in the header file of Clover 11 standard, the content is very detailed, interested friends can refer to it, I hope it can be helpful to you.

Introduces the classes and related functions in the header file of the Category 11 standard.

The header file contains the following classes and functions:

Providers class: std::promise, std::package_task Futures class: std::future, shared_future. Providers function: std::async () other types: std::future_error, std::future_errc, std::future_status, std::launch.

Introduction to the std::promise class

A promise object can hold a value of a type T that can be read by a future object (perhaps in another thread), so promise also provides a means of thread synchronization. When a promise object is constructed, it can be associated with a shared state (usually std::future), and a value of type T can be saved on the associated shared state (std::future).

You can get the future object associated with the promise object through get_future, and after calling this function, the two objects share the same shared state (shared state)

The promise object is an asynchronous Provider that sets the value of the shared state at some point. The future object can return the value of the shared state asynchronously or, if necessary, block the caller and wait for the shared state flag to change to ready before getting the shared state value.

Here is a simple example to illustrate the above relationship.

# include / / std::cout#include / / std::ref#include / / std::thread#include / / std::promise, std::futurevoid print_int (std::future& fut) {int x = fut.get (); / / get the value of the shared status. Std::cout

Tags: State objects functions threads associations files examples flags standards copies integers types allocation blocking content allocators more semantics headers preparation Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MySQL Shulou Tech Info Docker macOS Xiaomi