June 2018
Intermediate to advanced
348 pages
8h 45m
English
Now, let's discuss how we get a return value associated with a future into your code that needs results. The std::packaged_task is a template class that is available in the standard library to achieve task-based parallelism with the help of futures and promises. By setting up futures and promises in threads, it simplifies setting up a task without any explicit locks for sharing the result. A packaged_task instance provides a wrapper over std::thread to put the return value or exception caught into a promise. The member function get_future() in std::packaged_task will give you the future instance associated with the corresponding promise. Let's look at an example that uses a packaged task to find the sum of all elements ...