#include #include #include #include #include #include int main() { auto answer = std::async(std::launch::async, [] { return 6 * 7; }); bool ran = false; auto deferred = std::async(std::launch::deferred, [&] { ran = true; return 7; }); assert(!ran); const int delayed = deferred.get(); assert(ran && delayed == 7); assert(!deferred.valid()); const int value = answer.get(); assert(value == 42); std::promise promise; auto failure = promise.get_future(); std::thread worker([&] { try { throw std::invalid_argument("negative input"); } catch (...) { promise.set_exception(std::current_exception()); } }); worker.join(); bool caught = false; try { (void)failure.get(); } catch (const std::invalid_argument&) { caught = true; } assert(caught); std::cout << value << ' ' << delayed << ' ' << caught << '\n'; }