Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/146.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
尝试x秒,然后离开? 是否可以用C++编写TIME,catch语句,用以这样的方式,如果函数不能执行(它被卡住)程序就继续? < P>是的,你可以这样做,例如,_C++ - Fatal编程技术网

尝试x秒,然后离开? 是否可以用C++编写TIME,catch语句,用以这样的方式,如果函数不能执行(它被卡住)程序就继续? < P>是的,你可以这样做,例如,

尝试x秒,然后离开? 是否可以用C++编写TIME,catch语句,用以这样的方式,如果函数不能执行(它被卡住)程序就继续? < P>是的,你可以这样做,例如,,c++,C++,请特别注意函数。最可靠的方法是在子进程中执行工作,等待子进程超时,然后终止它。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 { //

请特别注意函数。

最可靠的方法是在子进程中执行工作,等待子进程超时,然后终止它。

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;
  }
clock_t start=clock();
const int max_try_clocks=5*时钟每秒;//5是秒数
//我们应该继续努力
请重试:
试一试{
//无论你需要尝试什么
}捕获(…){
如果(时钟()-开始
您可能需要启动一个单独的线程来完成工作并监视该线程。能否提供最少的代码示例。我无法理解一个程序是如何被卡住的,因为
try/catch
?如果你真正的意思是“卡住”而不是“花很长时间但确定的时间”,那么最好把它缩小到程序中可以阻塞的部分,并用非阻塞版本替换它。try/catch与代码被卡住有什么关系?如果代码花费的时间太长,是否希望引发异常?