#include <iostream>
#include <future>
#include <thread>
#include <chrono>
int longComputation(int x) {
std::this_thread::sleep_for(std::chrono::seconds(3)); // Simulate a long computation
return x * x;
}
int main() {
std::cout << "Starting async task with deferred execution..." << std::endl;
// Start the longComputation function with deferred execution
std::future<int> result = std::async(std::launch::deferred, longComputation, 10);
std::cout << "Doing other work in main thread..." << std::endl;
// The function is not executed until result.get() is called
int value = result.get();
std::cout << "Result from deferred task: " << value << std::endl;
return 0;
}