9. async, future, packaged_task, promise

std::async, std::future creates background tasks and returns values.

The thread is expected to return a value.

std::async is a function template that starts an asynchronous task and returns a std::future object

Asynchronous task: automatically create a thread and start executing the corresponding thread entry function, returning a std::future object.

This object contains the result returned by the thread function (the result returned by the thread), which can be obtained by calling the member function get() of the future object.

Future: It's also known as future, which provides a mechanism for accessing the results of asynchronous operations. This means that you may not be able to get the results immediately. In the near future, you will get the results after execution on site. It can be understood that the future object will save a value and at some point in the future. Enough to get.

 1 #include <future>
 2 #include <thread>
 3 
 4 using namespace std;
 5 
 6 int mythread(){
 7     cout<<std::this_thread::get_id()<<endl;
 8     std::chrono::milliseconds dura(5000);//Define 5 seconds
 9     std::this_thread::sleep_for(dura);//Five seconds'rest
10     return 5;
11 }
12 int main(){
13     cout << "main" << this_thread::get_id() <<endl;
14     std::future<int> result = std::async(mythread);//A thread is created. Similar to thread, the first parameter is thread function, the second parameter is object reference, and the third parameter is thread function parameter.
15     cout<<"continue..."<<endl;
16     int def;
17     def=0;
18     cout<<result.get()<<endl;//When the system executes here, it will get stuck here and wait for the sub-threads. After the threads return to 5, it will continue to go down.
19                             //It can only be invoked once, while invoking get It's going to go wrong.
20     //result.wait();Waiting for the thread to return itself does not return the result
21     cout<<"main over"<<endl;
22     return 0;
23     
24 }


You can also add a parameter at the front, which is std::lunch type (enumeration type), to achieve some special purposes;
Delayed invocation, without creating a new thread, is the thread entry function invoked in the main thread
std::launch::deferred: indicates that the thread entry function call is delayed until the wait() or get() function call of std::future is executed.
If wait or get is not invoked, then the thread is not executed or created at all.
std::future<int> result = std::async(std::lunch::deferred,mythread);

There is another parameter, std::launch::async, which is the default parameter. Threads are created when the async function is called.

 

std::packaged_task

Pack tasks and pack them up.

Is a class template, template parameters are various callable objects, through this function to wrap the callable object, convenient for future as a thread entry function to call.

 1 #include <future>
 2 #include <thread>
 3 
 4 using namespace std;
 5 
 6 int mythread(int& pt){
 7     cout << pt << endl;
 8     return 0;
 9 }
10 int main(){
11     //Writing 1:
12     std::packaged_task<int(int)> mypt(mythread);//Put function mythread adopt packaged_task Packing up,int(int): First int Represents the return type of a thread function, in parentheses int Is the parameter type of the thread function
13     std::thread t1(std::ref(mypt),1);//Threads start execution directly,The second parameter is the parameter of the thread entry function.
14     t1.join();
15     std::future<int> result = mypt.get_future();
16     cout<<result.get()<<endl;
17     return 0;
18     
19     //Writing II:use lambda Expression
20     //At this time, the top one mythread The function is deleted, which is equivalent to writing the thread entry function definition to this lambda It's in the expression.
21     std::packaged_task<int(int)> mypt([](int pt){
22         cout << pt << endl;
23         return 0;
24     
25     })
26 }

The function is:

You can use vector < std:: packaged_task < int (int)> mytask; // use a container to save all thread entry function objects

How do I add objects to the container?

std::packaged_task<int(int)> mypt(mythread);
Mytasks. push_back (std:: move (mypt); //move, mypt is empty

How do I get it out?

std::packaged_task<int(int)> mypt2;
auto iter = mytasks.begin();// Suppose there is only one object in this vector at this time
myptr2 = std::move(*iter);// mobile semantics
mytasks.erase(iter);
std::future<int> result = myptr2.get_future();
cout<<result.get()<<endl;

 

std::promise, class template

Give him a value in one thread, and you can use it in other threads.

 1 void mythread(std::promise<int>& tmp,int calc){
 2     //Do some complex operations
 3     int result = calc;//Preserve the results
 4     tmp.set_value(result);//The results are saved to tmp Object
 5     return;
 6 }
 7 
 8 int main(){
 9     std::promise<int> myprom;//Declare a promise Type object, saved value type is int
10     std::thread t1(mythread,std::ref(myprom),180);
11     t1.join();
12     
13     //Get the result value
14     std::future<int> ful = myprom.get_future();
15     auto result = ful.get();//get Call only once
16     cout<<result<<endl;
17 }

Keywords: PHP Lambda REST Mobile

Added by dar-k on Thu, 25 Jul 2019 10:46:32 +0300