#include #include #include #include //#include void Timeout() { boost::chrono::stopwatch<> stopwatch; try { boost::this_thread::sleep_for(boost::chrono::minutes(10)); std::cout << "Timeout!" << std::endl; std::cout << "Elapsed time: " << stopwatch.elapsed() << "." << std::endl; exit(31); } catch (const boost::thread_interrupted&) { //ok, interrupt was successful, we just let the thread go away. } } void run_test() { std::cout << "first test" << std::endl; for(int i = 0; i < 10000; ++i) { std::vector > threads; for(int j = 0; j < 50; ++j) { threads.push_back(boost::shared_ptr(new boost::thread(Timeout))); //interrupt some of the threads immediately if (j%4 == 0) { threads.back()->interrupt(); } } for (std::vector >::iterator it = threads.begin(); it != threads.end(); ++it) { (*it)->interrupt(); } while (!threads.empty()) { threads.back()->join(); threads.pop_back(); } } std::cout << "second test" << std::endl; double sleepTime = 0; double maxSleepTime = 0; for(int i = 0; i < 10000; ++i) { boost::chrono::stopwatch<> stopwatch; boost::thread thread(Timeout); thread.interrupt(); thread.join(); const double elapsed = stopwatch.elapsed().count() / 1000000000.0; sleepTime += elapsed; maxSleepTime = std::max(maxSleepTime, elapsed); if (elapsed > 30) //more than 30 seconds { std::cout << "Interrupt was too slow! elapsed: " << stopwatch.elapsed() << "." << std::endl; exit(14); } } std::cout << "Average interrupt time is " << sleepTime/10000*1000 << " milliseconds" << std::endl; std::cout << "Maximum interrupt time is " << maxSleepTime*1000 << " milliseconds" << std::endl; } int main() { try { run_test(); } catch(const std::exception& e) { std::cout << "Caught exception: " << e.what() << std::endl; return 1; } catch (...) { std::cout << "Caught ... exception" << std::endl; return 1; } return 1; }