/////////////////////////////////////////////////////////////////////// // MT1Q7.cpp - Meaning of Life // // // // Jim Fawcett, CSE687 - Object Oriented Design, Midterm Spring 2018 // /////////////////////////////////////////////////////////////////////// #include #include #include #include #include #include "../CppNoSqlDb/DateTime/DateTime.h" struct Result { size_t mol; std::string blahBlah; }; Result meaningOfLife(DateTime dt) { auto childThreadId = std::this_thread::get_id(); std::cout << "\n child thread id = " << childThreadId; std::this_thread::sleep_for(std::chrono::milliseconds(2000)); Result result; result.mol = 42; result.blahBlah = "The meaning of life on " + std::string(dt) + " is ,as any fool can see, ";; return result; } int main() { std::cout << "\n MT1Q7 - Meaning of life"; std::cout << "\n =========================\n"; auto mainThreadId = std::this_thread::get_id(); std::cout << "\n main thread id = " << mainThreadId; std::future fut = std::async(std::launch::async, meaningOfLife, DateTime().now()); Result result = fut.get(); std::cout << "\n " << result.blahBlah << result.mol << "!\n"; std::cout << "\n Thanks, Douglas Adams, for all the fish.\n\n"; //////////////////////////////////////////////////// // The code below runs the function asynchronously, // but does not make the result accessible. // // std::thread t(meaningOfLife, DateTime().now()); // t.join(); return 0; }