/**************************************************************** **** **** This file belongs with the course **** Introduction to Scientific Programming in C++/Fortran2003 **** copyright 2016=2023 Victor Eijkhout eijkhout@tacc.utexas.edu **** **** async.cxx : illustration of async **** **** https://stackoverflow.com/questions/12620186/futures-vs-promises **** ****************************************************************/ #include using std::cout; #include using std::stringstream; #include using std::string; #include using std::vector; #include #include #ifndef NTHREADS #define NTHREADS 5 #endif int main() { cout << "Single\n"; { //codesnippet cppasyncone std::future fut_str = std::async ( [] () -> string { return "Hello world"; } ); auto result_str = fut_str.get(); cout << result_str << '\n'; //codesnippet end } cout << "single\n"; cout << "Vector\n"; { //codesnippet cppasyncvec vector< std::future > futures; for ( int ithread=0; ithreadstring { stringstream ss; ss << "Hello world " << ithread; return ss.str(); } ) ); } for ( int ithread=0; ithread