C++ Mathematical Expression Toolkit (ExprTk) release
Loading...
Searching...
No Matches
Classes | Functions
exprtk_loop_timeout_rtc.cpp File Reference
#include <chrono>
#include <cstdio>
#include <string>
#include "exprtk.hpp"
Include dependency graph for exprtk_loop_timeout_rtc.cpp:

Go to the source code of this file.

Classes

struct  timeout_rtc_handler
 
class  timeout_rtc_handler::timeout_exception
 
struct  vector_access_rtc
 

Functions

template<typename T >
void loop_timeout_rtc ()
 
int main ()
 

Function Documentation

◆ loop_timeout_rtc()

template<typename T >
void loop_timeout_rtc ( )

Definition at line 85 of file exprtk_loop_timeout_rtc.cpp.

86{
87 typedef exprtk::expression<T> expression_t;
88 typedef exprtk::parser<T> parser_t;
89 typedef exprtk::loop_runtime_check loop_runtime_check_t;
90
91 const std::string sieve_of_eratosthenes_program =
92 " var sieve[10^8] := [false]; "
93 " var m := trunc(sqrt(sieve[])); "
94 " "
95 " sieve[0] := true; "
96 " sieve[1] := true; "
97 " "
98 " for (var i := 0; i <= m; i += 1) "
99 " { "
100 " if (false == sieve[i]) "
101 " { "
102 " for (var j := i^2; j < sieve[]; j += i) "
103 " { "
104 " sieve[j] := true; "
105 " } "
106 " } "
107 " }; "
108 " "
109 " var prime_count := sieve[] - sum(sieve); "
110 " "
111 " prime_count == 5761455; ";
112
113 timeout_rtc_handler loop_runtime_check;
114 loop_runtime_check.loop_set = loop_runtime_check_t::e_all_loops;
115 loop_runtime_check.max_loop_iterations =
116 static_cast<std::uint64_t>(10e8 * std::log10(std::log10(10e8))); // O(n) = nlog(log(n))
117
118 vector_access_rtc vec_rtc;
119
120 expression_t expression;
121
122 parser_t parser;
123
124 parser.register_loop_runtime_check(loop_runtime_check);
125 parser.register_vector_access_runtime_check(vec_rtc);
126
127 parser.compile(sieve_of_eratosthenes_program,expression);
128
129 const auto max_duration = std::chrono::seconds(1);
130 const auto timeout_tp = std::chrono::steady_clock::now() + max_duration;
131 loop_runtime_check.set_timeout_time(timeout_tp);
132
133 try
134 {
135 expression.value();
136 }
138 {
139 printf("Timeout Exception\n");
140 }
141 catch (std::runtime_error& rte)
142 {
143 printf("Exception: %s\n",rte.what());
144 }
145
146 parser.clear_loop_runtime_check();
147}
details::_uint64_t max_loop_iterations
Definition exprtk.hpp:2145
void set_timeout_time(const time_point_t &timeout_tp)

References exprtk::loop_runtime_check::loop_set, exprtk::loop_runtime_check::max_loop_iterations, and timeout_rtc_handler::set_timeout_time().

Here is the call graph for this function:

◆ main()

int main ( )

Definition at line 149 of file exprtk_loop_timeout_rtc.cpp.

150{
151 loop_timeout_rtc<double>();
152 return 0;
153}