How to use promise future in Category 11
This article will explain in detail how to use promise future in Craft 11. The content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
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