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