source:
doc/theses/andrew_beach_MMath/code/throw-detor.cpp@
e4ea4b8d
| Last change on this file since e4ea4b8d was f79ee0d, checked in by , 4 years ago | |
|---|---|
|
|
| File size: 1.1 KB | |
| Line | |
|---|---|
| 1 | // Throw Across Destructor |
| 2 | #include <chrono> |
| 3 | #include <cstdlib> |
| 4 | #include <exception> |
| 5 | #include <iostream> |
| 6 | #include <iomanip> |
| 7 | |
| 8 | using namespace std; |
| 9 | using namespace std::chrono; |
| 10 | |
| 11 | struct EmptyException : public std::exception {}; |
| 12 | |
| 13 | struct WithDestructor { |
| 14 | ~WithDestructor() { |
| 15 | asm volatile ("# destructor body"); |
| 16 | } |
| 17 | }; |
| 18 | |
| 19 | void unwind_destructor(unsigned int frames) { |
| 20 | if (frames) { |
| 21 | WithDestructor object; |
| 22 | unwind_destructor(frames - 1); |
| 23 | } else { |
| 24 | throw (EmptyException){}; |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | int main(int argc, char * argv[]) { |
| 29 | unsigned int times = 1; |
| 30 | unsigned int total_frames = 1; |
| 31 | if (1 < argc) { |
| 32 | times = strtol(argv[1], nullptr, 10); |
| 33 | } |
| 34 | if (2 < argc) { |
| 35 | total_frames = strtol(argv[2], nullptr, 10); |
| 36 | } |
| 37 | |
| 38 | time_point<steady_clock> start_time = steady_clock::now(); |
| 39 | for (int count = 0 ; count < times ; ++count) { |
| 40 | try { |
| 41 | unwind_destructor(total_frames); |
| 42 | } catch (EmptyException &) { |
| 43 | asm volatile ("# catch block"); |
| 44 | } |
| 45 | } |
| 46 | time_point<steady_clock> end_time = steady_clock::now(); |
| 47 | nanoseconds duration = duration_cast<nanoseconds>(end_time - start_time); |
| 48 | cout << "Run-Time (s): " << fixed << setprecision(1) << duration.count() / 1'000'000'000. << endl; |
| 49 | } |
Note:
See TracBrowser
for help on using the repository browser.