Given the following source code
#include <thread>
#include <future>
#include <iostream>
#include <string>
#include <chrono>
int main() {
auto task = std::async(std::launch::async, [] {
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
throw std::runtime_error("error");
});
try {
while (task.wait_for(std::chrono::seconds(0)) !=std::future_status::ready)
{
std::cout << "Not ready: " << std::endl;
}
task.get();
}
catch (const std::exception& e)
{
std::cout << "Valid: " << task.valid() << std::endl;
}
}
I would expect, that the program will response with Valid: 0
. Using g++ 6.2.0 this is the case. However, using MS VS2015 Version 14.0.25431.01 Update 3 the response is Valid: 1
. The state of the future is not invalidaded, after the exception was propagated to the main thread. Is this a bug or am i running into undefined behaviour here?