C ++ Concurrency and multithreading study notes --async, future, packaged_task, promise

  • async
  • future
  • packaged_task
  • promise

 

async

std: async function is used to start an asynchronous task, after starting up an asynchronous task and returns a std :: futre objects, start an asynchronous task is to automatically create a thread and starts executing the thread function corresponding entry, it returns a std :: future objects, the objects on the inside std :: future thread entry contains the results returned by the function. The results can be obtained by calling the member function get future object.

"Future" provides a mechanism for accessing the results of asynchronous operations, the results of the program is running, it may be no way to immediately get. It may take some time to return results. When the thread is finished, we can get a result. Therefore, it is so understanding, future years will save a value.

example:

The introduction of a header file

#include<future>

 Function to be executed, the thread entry function

int mythread()
{
cout << "my thread start, and thread id is " << std::this_thread::get_id() <<endl;
std::chrono::milliseconds dura(500);
std::this_thread::sleep_for(dura);
cout << "my thread end, and thread id is " << std::this_thread::get_id() <<endl;

return 5;

}

  In the main:

	cout << "Main Thread End!!! " <<std::this_thread::get_id() <<endl;
	
	std::future<int> result = std::async(mythread);

	cout << "continue..." << endl;

	int def = 0;
	def = 0;
	cout << result.get() << endl;
	cout << "Main thread end" << endl;

  When to get executed, the system will be stuck in here, have been waiting thread is finished. After the thread is finished, return. future, there are markers, mark the current thread has finished, the process can continue to go down. Binding relationship, future and create async thread binding together, we must wait for a thread of execution is over, the program can execute down.

future

get function does not return value to get the future, it is stuck in here waiting to get value, we must ensure that future and must be related to the function return value.

in the future there is a wait (), waiting thread returns, itself does not return a result. join ();

If multiple programs simultaneously write the get function, then it will error, get called only once!

In the std :: thread with a class member function as a thread's entry function, it can also be used with async class member function as a thread entry function.

std::future<int> result = std::async(&class::method, &obj, para1)

 From the third argument is the member function corresponding parameters. The second parameter is an object reference, in order to ensure the thread is used inside the same object, thread when performing a function which still corresponds to that object.

 

std::future<int> result = std::async(std::launch::deferred, &class::method, &obj, para1);

std::future<int> result = std::async(std::launch::async, &class::method, &obj, para1);

  

After the future if new objects, do not get () and wait (), the thread will still run, after the end of the thread running, the program exits.

get () member function returns the result of the implementation of the waiting thread end, we pass a parameter :: async () by additional to std, the parameter type is std :: launch types (enumerated types) to achieve some special purpose.

 

a) std :: launch :: deferred: indicates that the thread function calls are delayed entry into the std :: future of wait () or get () function call if you are performing. In fact there is no thread is created.

b) std :: launch :: async, began to create a thread in the time of the call, not wait until the get ().

Two marks can not be used together with the time must thoroughly understand the meaning of these flags.

packaged_task

std :: packaged_task: packaging tasks, or will wrap up the task, which is a class template. Template parameter is callable various parameters, you can put all kinds of objects can be called up by packaged_task packaging, facilitate future entry function as a thread calls.

In the main function, a function corresponding to the package, <int (int)> return value (parameter), the argument is the entry function, callable objects. we

std::packaged_task<int(int)>  mypt(mythread);

The function mythread by calling the following lines:

std::thread t1(std::ref(mypt), 1);

 Just create a thread is to wrap up the thread entry function, direct thread begins execution, the second parameter 1, as an argument thread entry function as a parameter mythread the entrance. By taking the results of future

std::future<int> result = mypt.get_future();

  After just bind, save the future which is the value of mypt inside. std :: future target included in the entry thread function returns the result, where result save mythread.

packaged_task packaged callable objects can also be called directly.

promise

std :: promise this is a class template, is to be able to give it a value in a thread, then this value is taken out. Be a very complex operation in the thread, then get the result of the operation.

void mythread(std::promise<int> &tmp, int calc);

  Function can be done in a series of complex operations, and then do other operations, spent a full 5s, a certain amount of time to rest, the results can be saved, then

Calc Result = int; 
tmp.set_value (Result); // save the results and return

  Tmp result is stored in the object, the object is declared a std :: promise in the main, this value can be saved, stored value of type int

:: Promise STD <int> myprom; 
STD :: Thread T1 (MyThread, STD :: REF (myprom), para); 

// End thread of execution and the like 
t1.join ();

  The results can be used to obtain other threads to go.

std :: future <int> fu1 = myprom.get_future (); // promise for acquiring and binding thread returns future value

  If you can get behind the front join immediately get this value.

auto result = fu1.get();

  

 

Guess you like

Origin www.cnblogs.com/rynerlute/p/11846171.html