1 // Copyright (C) 2012-2013 Vicente Botet 2 // 3 // Distributed under the Boost Software License, Version 1.0. (See accompanying 4 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 5 6 #include <boost/config.hpp> 7 #if ! defined BOOST_NO_CXX11_DECLTYPE 8 #define BOOST_RESULT_OF_USE_DECLTYPE 9 #endif 10 11 #define BOOST_THREAD_VERSION 4 12 //#define BOOST_THREAD_USES_LOG 13 #define BOOST_THREAD_USES_LOG_THREAD_ID 14 15 #include <boost/thread/detail/log.hpp> 16 #include <boost/thread/future.hpp> 17 #include <boost/assert.hpp> 18 #include <exception> 19 #include <string> 20 #include <iostream> 21 22 #if defined BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION 23 24 #ifdef BOOST_MSVC 25 #pragma warning(disable: 4127) // conditional expression is constant 26 #endif 27 p1_ex()28int p1_ex() 29 { 30 BOOST_THREAD_LOG << "P1" << BOOST_THREAD_END_LOG; 31 throw std::logic_error("kk"); 32 } 33 p1()34int p1() 35 { 36 BOOST_THREAD_LOG << "P1" << BOOST_THREAD_END_LOG; 37 return 1;; 38 } 39 main()40int main() 41 { 42 const int number_of_tests = 200; 43 BOOST_THREAD_LOG << "<MAIN" << BOOST_THREAD_END_LOG; 44 45 { 46 for (int i=0; i< number_of_tests; i++) 47 try 48 { 49 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 50 boost::future<int> f1 = boost::async(boost::launch::async, &p1); 51 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 52 f1.wait(); 53 BOOST_ASSERT(f1.get()==1); 54 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 55 } 56 catch (std::exception& ex) 57 { 58 std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; 59 BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; 60 return 1; 61 } 62 catch (...) 63 { 64 std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; 65 BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; 66 return 2; 67 } 68 } 69 70 { 71 for (int i=0; i< number_of_tests; i++) 72 try 73 { 74 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 75 boost::future<int> f1 = boost::async(&p1); 76 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 77 boost::future<int> f2 = f1.fallback_to(-1); 78 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 79 f2.wait(); 80 //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; 81 BOOST_ASSERT(f2.get()==1); 82 //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; 83 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 84 } 85 catch (std::exception& ex) 86 { 87 std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; 88 BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; 89 return 1; 90 } 91 catch (...) 92 { 93 std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; 94 BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; 95 return 2; 96 } 97 } 98 99 { 100 for (int i=0; i< number_of_tests; i++) 101 try 102 { 103 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 104 boost::future<int> f1 = boost::async(boost::launch::async, &p1_ex); 105 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 106 f1.wait(); 107 BOOST_ASSERT(f1.get_or(-1)==-1); 108 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 109 } 110 catch (std::exception& ex) 111 { 112 std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; 113 BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; 114 return 1; 115 } 116 catch (...) 117 { 118 std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; 119 BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; 120 return 2; 121 } 122 } 123 124 { 125 for (int i=0; i< number_of_tests; i++) 126 try 127 { 128 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 129 boost::future<int> f1 = boost::async(boost::launch::async, &p1_ex); 130 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 131 boost::future<int> f2 = f1.fallback_to(-1); 132 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 133 f2.wait(); 134 //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; 135 BOOST_ASSERT(f2.get()==-1); 136 //std::cout << __FILE__ << "["<< __LINE__<<"] " << std::endl; 137 BOOST_THREAD_LOG << "" << BOOST_THREAD_END_LOG; 138 } 139 catch (std::exception& ex) 140 { 141 std::cout << __FILE__ << "["<< __LINE__<<"] " << "ERRORRRRR "<<ex.what() << "" << std::endl; 142 BOOST_THREAD_LOG << "ERRORRRRR "<<ex.what() << "" << BOOST_THREAD_END_LOG; 143 return 1; 144 } 145 catch (...) 146 { 147 std::cout << __FILE__ << "["<< __LINE__<<"] " << " ERRORRRRR exception thrown" << std::endl; 148 BOOST_THREAD_LOG << " ERRORRRRR exception thrown" << BOOST_THREAD_END_LOG; 149 return 2; 150 } 151 } 152 BOOST_THREAD_LOG << "MAIN>" << BOOST_THREAD_END_LOG; 153 return 0; 154 } 155 #else 156 main()157int main() 158 { 159 return 0; 160 } 161 #endif 162