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