Medium
Discarding the future returned by std::async
measure() times how long the block containing the std::async call takes to run. slowTask() sleeps for ~800ms before returning. What does this program print?
main.cpp
int slowTask() {
std::this_thread::sleep_for(std::chrono::milliseconds(800));
return 42;
}
int main() {
auto start = std::chrono::steady_clock::now();
{
std::async(std::launch::async, slowTask); // return value discarded
}
auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(
std::chrono::steady_clock::now() - start).count();
std::cout << "elapsed: " << ms << "ms" << std::endl;
}29 of 51 in Concurrency