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.

Solution: Download images

Solution: Download images

(lively music) - [Instructor] For our solution to the download images challenge, we considered each call to the download image helper function as separate tasks that we could run asynchronously. And since we would need to get the return value with the number of bytes from each of those tasks, it seemed like the perfect use case for futures. So, we create a list on line 26 to hold our futures, then use a for loop to start a bunch of asynchronous tasks to execute the download image function for each image and store the resulting feature object in the list. After that, a second for loop on line 30 iterates through the list of futures using the get function to retrieve their return values and then adding them to the total bytes accumulator variable. Finally, when all of the futures are complete and their individual results added together, the function will return the total number of bytes downloaded on line 34. Scrolling down to the main function, on lines 67 and 68, we have the program…

Contents