What is the purpose of .then construction in PPL tasks?

Oleksandr Karaberov picture Oleksandr Karaberov · Nov 4, 2012 · Viewed 7.9k times · Source

I'm interesting what is the purpose of .then construction in PPL and where can I test it? It seems Visual Studio 2012 don't support it yet (may be some future CTP's?). And does it have equivalents in standard C++11 async library?

Answer

Klaim picture Klaim · Nov 4, 2012

The purpose is for you to be able to express asynchronous tasks that have to be executed in sequence.

For example, let's say I'm in a GUI application. When the user presses a button, I want to start a task asynchronously to retrieve a file online, then process it to retrieve some kind of data, then use this data to update the GUI. While this is happening, there are tons of other tasks going on, mainly to keep the GUI responsive.

This can be done by using callbacks that call callbacks. The .then() feature associated with lambdas allows you to write all the callbacks content where you instantiate it (you can still use separate callbacks if you want). It also don't guarantee that the work of each separate task will be done by the same thread, making possible for free threads to steal the tasks if the initial thread have too much work to do already.

The .then() function doesn't exists in C++11 but it is proposed to be added to the std::future class (that is basically a handle to a task or task result).