(1) std :: async, std :: future to create background tasks and return
(2) std :: packaged_task
(3) std :: Promise
(4) Summary of
a, std :: async, std :: future and create background tasks return
hopes thread returns a result;
std :: the async function is a template used to start an asynchronous task, after starting up an asynchronous task,
he returned to a std :: future objects, std :: future is a class template
what is start an asynchronous task ah?
That automatically creates a thread and starts executing the corresponding thread entry function, which returns a
std :: future object that contains the results returned by the function thread (the thread return results).
You can get the results by the member function get std :: future object ().
std :: future provide a mechanism to access the results of asynchronous operations, the results can not be obtained immediately,
but when the thread finished executing, you can get to the results, future objects in a stored value, is the result,
#include <future>
#include <list>
#include <map>
#include <mutex>
#include <thread>
int thread_fun()//线程入口函数
{
cout<<"thread_fun start thread_id = "<<std::this_thread::get_id()<<endl;
std::chrono::milliseconds dura(5000);//休息5秒
std::this_thread::sleep_for(dura);//休息一定时长
cout<<"thread_fun end thread_id = "<<std::this_thread::get_id()<<endl;
return 5;
}
main int ()
{
// std :: future following program objects get () member function to wait for the result of thread execution and returns the result
cout << "main thread_id =" << std :: :: this_thread get_id () << endl;
std :: future <int> result = std :: async (thread_fun); // create a thread, and begin to implement
the binding relationship, the process will not be stuck in here
cout << "the Continue ..." << endl;
int DEF = 0;
cout << result.get () << endl; // will first card in the get () here, wait until thread_fun executed, to obtain the return value, then threaded program will continue downward.
//result.wait();// waiting thread returns, itself does not return result;
cout << "End ..." << endl;
return 0;
}
class B
{
public:
int thread_fun(int my_mun)//线程入口函数
{
cout<<my_mun<<endl;
cout<<"thread_fun start thread_id = "<<std::this_thread::get_id()<<endl;
std::chrono::milliseconds dura(5000);//休息5秒
std::this_thread::sleep_for(dura);//休息一定时长
cout<<"thread_fun end thread_id = "<<std::this_thread::get_id()<<endl;
return 5;
}
}
main int ()
{
A A1;
int = 12 is Mun;
// std :: future program following objects get () member function and return the execution result of waiting threads result
cout << "main thread_id =" << std :: this_thread :: get_id () << endl;
std :: Future <int> the Result = std :: the async (& a :: thread_fun, & a, NUM); // create a thread, and begin to implement
the binding relationship, the process will not be stuck in here
<< COUT "Continue ..." << endl;
int DEF = 0;
cout << result.get () << endl; // will first card in the get () here, wait until thread_fun executed, to obtain the return value, then threaded program will continue downward.
//result.wait();// waiting thread returns, itself does not return result;
cout << "End ..." << endl;
return 0;
}