diff --git a/cpp/stl/multiple_shared_future_get.cpp b/cpp/stl/multiple_shared_future_get.cpp new file mode 100644 index 0000000..fd08061 --- /dev/null +++ b/cpp/stl/multiple_shared_future_get.cpp @@ -0,0 +1,54 @@ +/* +VIM: let b:lcppflags="-std=c++14 -O2 -pthread -I." +VIM: let b:wcppflags="/O2 /EHsc /DWIN32" +VIM-: let b:cppflags=g:Iboost.g:Itbb +VIM-: let b:ldflags=g:Lboost.g:Ltbb +VIM-: let b:ldlibpath=g:Bboost.g:Btbb +VIM-: let b:argv="" +*/ +#include +#include +#include +#include + +int main ( void ) +{try{ + auto begin = std::chrono::high_resolution_clock::now(); + + std::promise p; + auto f = p.get_future().share(); + + std::thread t1([f](){ + std::cout << ' ' << f.get() << std::endl; + }); + + std::thread t2([f](){ + std::cout << ' ' << f.get() << std::endl; + }); + + + p.set_value(55); + + t1.join(); + t2.join(); + + //...... + + auto end = std::chrono::high_resolution_clock::now(); + std::chrono::duration seconds = end - begin; + std::cout << "Time: " << seconds.count() << std::endl; + return 0; +} +catch ( const std::exception& e ) +{ + std::cerr << std::endl + << "std::exception(\"" << e.what() << "\")." << std::endl; + return 2; +} +catch ( ... ) +{ + std::cerr << std::endl + << "unknown exception." << std::endl; + return 1; +}} +