C++ multi-threaded learning 10 promise and future multi-threaded asynchronous value transfer

The execution of the process is intermittent, and the process moves forward at a grid-independent and unpredictable speed. It is undefined when a result in a thread is obtained.
If you want to obtain the definite calculation result of the thread, you can also use the previous lock strategy, but it is more troublesome, so it is necessary to use more concise promise and future

Assuming that thread 1 needs the data of thread 2, the combined usage is as follows:

Thread 1 initializes a promise object and a future object, and the promise is passed to thread 2, which is equivalent to a promise made by thread 2 to thread 1; future is equivalent to accepting a promise to obtain the value passed by thread 2 in the future. Thread 2 gets the
promise Finally, you need to pass relevant data to this promise, and then the future of thread 1 can get the data.
If thread 1 wants to get data, but thread 2 does not give data, thread 1 blocks until the data of thread 2 arrives.
A simple explanation process:
insert image description here
source: graphic future and promise

future_status has three states:
(1) deferred: the asynchronous operation has not started
(2) ready: the asynchronous operation has been completed
(3) timeout: the asynchronous operation has timed out
.
Future provides some functions such as get(), wait(), wait_for():
(1) get() to get the result of future, if the asynchronous operation is not over yet, it will wait here for set_value() to set the shared state value, after which the shared status flag of the promise becomes ready, and the returned result is obtained.
(2) wait() just waits for the end of the asynchronous operation here, and cannot get the return result.
(3) wait_for() waits for a timeout to return the result.
Reference: https://zhuanlan.zhihu.com/p/448035015

To introduce the future library

#include <thread>
#include <iostream>
#include <future>
#include <string>
using namespace std;

void TestFuture(promise<string> p)
{
    
    
    cout << "begin TestFuture" << endl;
    this_thread::sleep_for(3s);
    cout << "begin set value" << endl;
    p.set_value("TestFuture value");
    this_thread::sleep_for(3s);
    cout << "end TestFuture" << endl;
}

int main(int argc, char* argv[])
{
    
    
    //异步传输变量存储
    promise<string> p;
    //用来获取线程异步值获取
    auto future = p.get_future();
    auto th = thread(TestFuture, move(p));
 
        cout << "future get() = " << future.get() << endl;

    th.join();
    getchar();
    return 0;
}

First create a test thread’s promise p to the main thread, then obtain the future object from p, use this object as a token to receive the value generated by the promise P, pass this promise to the main thread when creating the thread, and then the main thread wants To get the token value, the main thread will wait in get before the test thread sets the value on the promise, and return this value after setting the value:
insert image description here
Note that when using the promise mechanism to create a thread, the promise type is std::promisestd::string&& , that is, an rvalue reference, so you need to use move() so that P can be used as an rvalue here, so that it can match an rvalue reference:
insert image description here

Guess you like

Origin blog.csdn.net/qq_42567607/article/details/126075571