Try for x seconds, then leave?
Is it po开发者_如何学编程ssible to write in C++ try,catch statement with timer in that way that if function is not able to be executed (it's stuck) program just continues?
Yes, you could do this with, for example, Boost.Thread.
Look especially at the timed_join function.
The most robust approach is to perform the work in a sub-process, wait for the sub-process with a timeout, then kill it.
KISS:
clock_t start = clock();
const int max_try_clocks = 5 * CLOCKS_PER_SEC; // 5 is the number of seconds
// we should keep trying for
try_again:
try {
// whatever you need to try
} catch (...) {
if (clock() - start < max_try_clocks)
goto try_again;
}
精彩评论