| 1 | #include <iostream>
|
|---|
| 2 | #include <boost/thread.hpp>
|
|---|
| 3 | #include <boost/chrono/stopwatches/stopwatch.hpp>
|
|---|
| 4 | #include <boost/chrono/chrono_io.hpp>
|
|---|
| 5 | //#include <boost/timer/timer.hpp>
|
|---|
| 6 |
|
|---|
| 7 | void Timeout()
|
|---|
| 8 | {
|
|---|
| 9 | boost::chrono::stopwatch<> stopwatch;
|
|---|
| 10 | try
|
|---|
| 11 | {
|
|---|
| 12 | boost::this_thread::sleep_for(boost::chrono::minutes(10));
|
|---|
| 13 |
|
|---|
| 14 | std::cout << "Timeout!" << std::endl;
|
|---|
| 15 | std::cout << "Elapsed time: " << stopwatch.elapsed() << "." << std::endl;
|
|---|
| 16 | exit(31);
|
|---|
| 17 | }
|
|---|
| 18 | catch (const boost::thread_interrupted&)
|
|---|
| 19 | {
|
|---|
| 20 | //ok, interrupt was successful, we just let the thread go away.
|
|---|
| 21 | }
|
|---|
| 22 | }
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 | void run_test()
|
|---|
| 26 | {
|
|---|
| 27 |
|
|---|
| 28 | std::cout << "first test" << std::endl;
|
|---|
| 29 | for(int i = 0; i < 10000; ++i)
|
|---|
| 30 | {
|
|---|
| 31 | std::vector<boost::shared_ptr<boost::thread> > threads;
|
|---|
| 32 |
|
|---|
| 33 | for(int j = 0; j < 50; ++j)
|
|---|
| 34 | {
|
|---|
| 35 | threads.push_back(boost::shared_ptr<boost::thread>(new boost::thread(Timeout)));
|
|---|
| 36 |
|
|---|
| 37 | //interrupt some of the threads immediately
|
|---|
| 38 | if (j%4 == 0)
|
|---|
| 39 | {
|
|---|
| 40 | threads.back()->interrupt();
|
|---|
| 41 | }
|
|---|
| 42 | }
|
|---|
| 43 |
|
|---|
| 44 | for (std::vector<boost::shared_ptr<boost::thread> >::iterator it = threads.begin(); it != threads.end(); ++it)
|
|---|
| 45 | {
|
|---|
| 46 | (*it)->interrupt();
|
|---|
| 47 | }
|
|---|
| 48 |
|
|---|
| 49 | while (!threads.empty())
|
|---|
| 50 | {
|
|---|
| 51 | threads.back()->join();
|
|---|
| 52 | threads.pop_back();
|
|---|
| 53 | }
|
|---|
| 54 | }
|
|---|
| 55 |
|
|---|
| 56 | std::cout << "second test" << std::endl;
|
|---|
| 57 | double sleepTime = 0;
|
|---|
| 58 | double maxSleepTime = 0;
|
|---|
| 59 | for(int i = 0; i < 10000; ++i)
|
|---|
| 60 | {
|
|---|
| 61 | boost::chrono::stopwatch<> stopwatch;
|
|---|
| 62 |
|
|---|
| 63 | boost::thread thread(Timeout);
|
|---|
| 64 | thread.interrupt();
|
|---|
| 65 | thread.join();
|
|---|
| 66 | const double elapsed = stopwatch.elapsed().count() / 1000000000.0;
|
|---|
| 67 | sleepTime += elapsed;
|
|---|
| 68 | maxSleepTime = std::max(maxSleepTime, elapsed);
|
|---|
| 69 |
|
|---|
| 70 | if (elapsed > 30) //more than 30 seconds
|
|---|
| 71 | {
|
|---|
| 72 | std::cout << "Interrupt was too slow! elapsed: " << stopwatch.elapsed() << "." << std::endl;
|
|---|
| 73 | exit(14);
|
|---|
| 74 | }
|
|---|
| 75 | }
|
|---|
| 76 |
|
|---|
| 77 | std::cout << "Average interrupt time is " << sleepTime/10000*1000 << " milliseconds" << std::endl;
|
|---|
| 78 | std::cout << "Maximum interrupt time is " << maxSleepTime*1000 << " milliseconds" << std::endl;
|
|---|
| 79 | }
|
|---|
| 80 |
|
|---|
| 81 | int main()
|
|---|
| 82 | {
|
|---|
| 83 | try
|
|---|
| 84 | {
|
|---|
| 85 | run_test();
|
|---|
| 86 | }
|
|---|
| 87 | catch(const std::exception& e)
|
|---|
| 88 | {
|
|---|
| 89 | std::cout << "Caught exception: " << e.what() << std::endl;
|
|---|
| 90 | return 1;
|
|---|
| 91 | }
|
|---|
| 92 | catch (...)
|
|---|
| 93 | {
|
|---|
| 94 | std::cout << "Caught ... exception" << std::endl;
|
|---|
| 95 | return 1;
|
|---|
| 96 | }
|
|---|
| 97 | return 1;
|
|---|
| 98 | }
|
|---|