#if defined(WIN32) && ! defined(USE_GENERIC) # define USE_GENERIC #endif #ifdef USE_GENERIC # include // using std::thread, std::this_thread # include // using std::chrono::seconds # include // using std::raise, SIGABRT, SIGXCPU #else # include # include // using setrlimit # include // using std::min # include // using std::system_error, std::system_category # include // using errno #endif namespace ext { #ifdef USE_GENERIC namespace { void sleeper_thread(int seconds) { std::this_thread::sleep_for(std::chrono::seconds(seconds)); int signal; # ifdef SIGXCPU signal = SIGXCPU; # else signal = SIGABRT; # endif std::raise(signal); } } void limit_time(int seconds) { std::thread(sleeper_thread, seconds).detach(); } #else void limit_time(int seconds) { rlimit limit; if(getrlimit(RLIMIT_CPU, &limit)) goto err; if(limit.rlim_max > static_cast(seconds)) limit.rlim_max = seconds; limit.rlim_cur = limit.rlim_max; if(setrlimit(RLIMIT_CPU, &limit)) goto err; return; err: throw std::system_error(errno, std::system_category(), "rlimit"); } #endif } int main() { ext::limit_time(10); int j = 1; for(unsigned i = 10; i >= 0; --i) // accidental infinite loop j *= 2; return j; }