Welcome to our deep dive into std::future in C++! Today, we'll explore the world of asynchronous programming and learn how std::future helps us run functions concurrently, making our programs more efficient and responsive.
Asynchronous programming allows your program to execute multiple tasks simultaneously. Instead of waiting for one task to complete before starting another, it enables tasks to run concurrently, improving performance and user experience.
std::future š”std::future is a template class in the C++ Standard Library that represents the result of an asynchronous computation. It acts as a container for the result of an asynchronous function call and can be used to wait for its completion and retrieve the result.
std::async Function šThe std::async function is used to launch a function asynchronously. It returns a std::future object that represents the asynchronous task.
#include <future>
#include <iostream>
std::future<int> asyncSum(int a, int b) {
return std::async(std::launch::async, [](int x, int y) {
return x + y;
}, a, b);
}In this example, we define a function asyncSum that takes two numbers a and b and returns their sum asynchronously. The std::launch::async parameter ensures that the function is launched concurrently.
std::future šOnce we have a std::future object, we can wait for its completion and retrieve the result using the get() method.
#include <iostream>
#include <future>
int main() {
std::future<int> result = asyncSum(5, 7);
std::cout << "Performing other tasks..." << std::endl;
int sum = result.get();
std::cout << "Sum: " << sum << std::endl;
return 0;
}In this example, we wait for the result of the asynchronous sum operation by calling get() on the std::future object result. The std::cout statements demonstrate that the main function can continue executing while the sum operation is running concurrently.
std::future š”std::future provides an exception-safe way to handle errors that may occur during asynchronous computations. If an error occurs, std::future::get() throws a std::future_error exception.
Let's put std::future into action by implementing an asynchronous web scraper. This example scrapes the titles of the first 10 pages of Reddit's "Programming" subreddit using the libcurl library.
#include <iostream>
#include <future>
#include <vector>
#include <curl/curl.h>
#include <json/json.h>
#include <sstream>
std::vector<std::string> scrapePage(int page) {
// ... (Implementation of the web scraping function)
}
std::future<std::vector<std::string>> asyncScrapePage(int page) {
return std::async(std::launch::async, [](int page) {
return scrapePage(page);
}, page);
}
int main() {
std::vector<std::future<std::vector<std::string>>> pages;
for (int i = 1; i <= 10; ++i) {
pages.push_back(asyncScrapePage(i));
}
std::vector<std::vector<std::string>> results;
for (auto& future : pages) {
results.push_back(future.get());
}
for (const auto& posts : results) {
for (const auto& post : posts) {
std::cout << post << std::endl;
}
}
return 0;
}In this example, we create 10 asynchronous tasks that scrape the titles of individual pages from Reddit's "Programming" subreddit. We then wait for all the tasks to complete and print the results.
What is the primary use of the `std::future` class in C++?
That's it for today! We hope you enjoyed learning about std::future and gained a better understanding of asynchronous programming in C++. Stay tuned for more tutorials on CodeYourCraft! šÆ