From the course: Complete Guide to Parallel and Concurrent Programming with C++

Unlock this course with a free trial

Join today to access over 24,800 courses taught by industry experts.

Future: C++ demo

Future: C++ demo

- 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 Baron 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 it to run the function asynchronously. And then the second argument is the…

Contents