#include#include int main() { // Create a future task that returns a boolean value std::future future = std::async(std::launch::async, []() { std::cout << "Executing future task..." << std::endl; return true; }); // Get the task_id of the future task std::future task_id = future.get_id(); std::cout << "Task ID: " << task_id << std::endl; return 0; }
#includeThis code creates a future task that waits for 3 seconds and returns an integer value. It then waits for the future task to complete, obtains its task_id, and gets the result of the future task. Package/library used: The standard C++ library (header files#include #include int main() { // Create a future task that waits for 3 seconds and returns an integer value std::future future = std::async(std::launch::async, []() { std::cout << "Executing future task..." << std::endl; std::this_thread::sleep_for(std::chrono::seconds(3)); return 42; }); // Wait for the future task to complete future.wait(); // Get the task_id of the future task std::future task_id = future.get_id(); std::cout << "Task ID: " << task_id << std::endl; // Get the result of the future task int result = future.get(); std::cout << "Result: " << result << std::endl; return 0; }