Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report