开发者

Try for x seconds, then leave?

开发者 https://www.devze.com 2023-04-03 11:18 出处:网络
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,

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;
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号