#include <iostream>
#include <thread>
#include <functional>
// A function that simulates an asynchronous task and uses a callback
void asyncTask(int x, std::function<void(int)> callback) {
std::thread([=]() {
std::this_thread::sleep_for(std::chrono::seconds(2)); // Simulate a delay
callback(x * 2); // Invoke the callback after the delay
}).detach(); // Detach the thread to run independently
}
int main() {
std::cout << "Starting async task..." << std::endl;
asyncTask(5, [](int result) {
std::cout << "Async task completed with result: " << result << std::endl;
});
std::this_thread::sleep_for(std::chrono::seconds(3)); // Wait for the async task to complete
return 0;
}