Junior — Middle
Have you used std::async in your projects for asynchronous operations?
sobes.tech AI
Answer from AI
Yes, I have used std::async for executing asynchronous tasks in C++. This mechanism allows you to run a function in a separate thread and obtain the result via std::future. It is convenient for parallelizing computations without explicit thread management.
Example usage:
#include <iostream>
#include <future>
int compute(int x) {
return x * x;
}
int main() {
std::future<int> result = std::async(std::launch::async, compute, 10);
std::cout << "Result: " << result.get() << std::endl;
return 0;
}
Here, std::async runs the compute function asynchronously, and result.get() blocks until the result is available.