1. < future > Head file Introduction
- Classes
Std::future
Std::future_error
std::p ackaged_task
std::p romise
Std::shared_future
- Functions
Std::async
Std::future_category
2. Std::future
Simply put, Std::future provides a mechanism for accessing the results of asynchronous operations.
From a literal point of view, it represents the future. Usually an asynchronous operation we are not able to get the results of the operation immediately, can only be acquired at some point in the future. We can get the results in a synchronous wait, and we can get the result of the asynchronous operation by querying the state of the Future (Future_status). There are three states of Future_status:
Deferred: Asynchronous operation hasn't started yet
Ready: Asynchronous operation completed
Timeout: Asynchronous operation timed out
There are three ways to get a future result: get, wait, wait_for, where get waits for an asynchronous operation to end and returns a result, wait waits for the asynchronous operation to complete, no return value, and wait_for is timed out waiting for the result to return.
Example:
//Query the status of the futureStd:: Future_statusStatus Do{Status=Future.WAIT_FOR (Std:: Chrono:: Seconds(1));if(Status==Std:: Future_status::d eferred) {STD:: cout << "deferred\n"; }Else if(Status==Std:: Future_status:: Timeout) {STD:: cout << "timeout\n"; }Else if(Status==Std:: Future_status:: Ready) {STD:: cout << "ready!\n"; } } while(Status!=Std:: Future_status:: Ready);
3. std::p romise
The Promise object can hold a value of type T, which can be read by the future object (possibly in another thread), a means by which promise provides synchronization. When constructing promise, the promise object can be associated with a shared state, which can store a value of type T or a class derived from std::exception, and can be get_future to get the object associated with the Promise object. After the function is called, two objects share the same shared state.
The Promise object is an asynchronous provider that can set the value of a shared state at a certain point in time.
The future object can return a shared state value, or, if necessary, block the caller and wait for the shared status identity to become ready before the shared status can be obtained.
Example:
#include <iostream> //std::cout #include <functional> //Std::ref #include <thread> //Std::thread #include <future> /std::p romise, Std::future voidPrint_int (STD::future<int>& fut) {intx = Fut.get ();//Gets the value of the shared state. STD::cout<<"Value:"<< x <<' \ n ';//Print value:10.}intMain () {STD::p romise<int> Prom;//Generates an STD::p romise<int> object. STD::future<int> fut = prom.get_future ();//And future association. STD:: Thread T (print_int,STD:: Ref (FUT));//Give the future to another thread T.Prom.set_value (Ten);//Set the value of shared state, here and thread t remain in sync.T.join ();return 0;}
std::p Romise Constructor
| constructor function |
|
| Default (1) |
Promise (); |
| With Allocator (2) |
Template Promise (allocator_arg_t AA, const alloc& Alloc); |
| copy [deleted] (3) |
Promise (const promise&) = delete; |
| Move (4) |
Promise (promise&& x) noexcept; |
- 1. The default constructor initializes an empty shared state.
- 2. The constructor with a custom memory allocator is similar to the default constructor, but uses a custom allocator to assign shared state.
- 3. Copy constructor, disabled.
- 4. Move the constructor.
In addition, std::p romise operator= No copy semantics, that is std::p romise Normal assignment operation is disabled, operator= only move semantics, so std::p romise object is forbidden to copy.
std::p romise member function
std::p romise::get_future: Returns a future object associated with the promise shared state
std::p romise::set_value: Sets a value for shared status, after which the promise shared status identity becomes ready
std::p romise::set_exception: Set an exception for promise, after which the shared status identity of promise becomes ready
std::p romise::set_value_at_thread_exit: Sets a value for shared state, but does not set the flag for shared status to ready, and when the thread exits, the Promise object is automatically set to Ready (note: The thread has set the value of promise and will throw a Future_error (promise_already_satisfied) exception if there are other actions that modify the shared state value after the threads end)
std::p romise::swap: Exchange Promise Sharing status
4. std::p ackaged_task
std::p Ackaged_task Wraps a callable target (such as function, lambda expression, bind expression, or another function object) so that it can be called asynchronously, It's kind of like promise to a certain extent, promise saves a shared state value, and Packaged_task saves a function.
std::packaged_task<intreturn7; }); std::thread t1(std::ref(task)); std::future<int> f1 = task.get_future(); auto r1 = f1.get();
5. Summary
Promise,future and callback are often used as a set of non-blocking models in concurrent programming. Where the future represents a "result" of an asynchronous task that may not have actually been completed, the result can be added Callback to perform the corresponding operation after the task execution succeeds or fails, and Promise to the task performer, the task performer passes Promise You can mark the completion or failure of a task.
6. Std::async
Std::async Approximate working process: The asynchronous operation is packaged with std::p ackaged_task, and then the result of the asynchronous operation is placed in std::p romise, this process is the process of creating the future. Outside again through the future.get/wait to obtain this future result.
It can be said that Std::async help us to Std::future, std::p romise and std::p ackaged_task three combined together.
Std::async's prototype:
async(std::launch::async| std::launch::deferred, f, args...)
The first parameter is the thread's creation policy, and the default policy is to create the thread immediately:
Std::launch::async: Start creating threads when Async is called.
Std::launch::d eferred: Lazy load mode creates a thread. When Async is called, the thread is not created until a future get or wait is called.
The second argument is a thread function, followed by a parameter that is a thread function.
A simple example:
std::future<int>F1 =std:: Async(std:: Launch:: Async, [](){return 8; }); Cout<<f1.get () <<endl;//output: 8 std::future<int>F2 =std:: Async(std:: Launch:: Async, [] () {cout<<8<<endl; }); F2.wait ();//output: 8
C++11 Multithreading Future/promise Introduction