std::packaged_task
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 ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access