From the course: Parallel and Concurrent Programming with C++ Part 2

Unlock the full course today

Join today to access over 22,400 courses taught by industry experts or purchase this course individually.

Future: C++ demo

Future: C++ demo - C++ Tutorial

From the course: Parallel and Concurrent Programming with C++ Part 2

Start my 1-month free trial

Future: C++ demo

- [Instructor] To demonstrate using futures in C++, let's recreate my interactions with Olivia from the previous video, starting from this basic shell of a program. First, we'll need to include the future header at the top of the program. Then, we'll create a new function named how_many_vegetables to serve as the task to execute. That function will print a message that Olivia is counting vegetables, then it will sleep for three seconds to simulate the time it takes for Olivia to complete that task, and finally, the function will return a value for the number of vegetables in the pantry. For simplicity, let's say 42 is always the answer. Now, down in the main function, we'll print a message at the beginning of the program when Barron asks Olivia how many vegetables are in the pantry. Then, we'll use the async function to asynchronously execute how_many_vegetables. The first argument is the launch policy and we'll tell…

Contents