如果它们没有结束运行,则在 5 分钟内停止功能 [英] Stop functions in 5 minutes if they dont end running

查看:30
本文介绍了如果它们没有结束运行,则在 5 分钟内停止功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在我的项目中添加一个功能.

I want to add a feature in my project.

我有 2 个函数在 for 循环中运行,因为我想在随机数组中找到函数的解.我还有一个生成随机数组的函数.在每个循环中,由 random_array fun 组成的数组是 2 个函数的输入.这两个函数在屏幕上打印解决方案,它们不返回参数.

I have 2 functions running in a for-loop because I want to find the solution of the functions in random arrays. I also have an function that makes random arrays. In each loop the array that is made by the random_array fun are the input of the 2 functions. The 2 functions print solutions in the screen , they dont return an argument.

int main(){
    for (i=0;i<50 i++)
    {
    arr1=ramdom_array();
    func1(arr1)
    func2(arr1)
    }
}

如果函数在 5 分钟内没有结束,我需要停止运行.我想我必须把这样的功能放进去:

I need to stop the functions running if they have not ended in 5 minutes. I have thought that I have to put in the functions something like this :

void func1(array<array<int,4>,4> initial)
{
while (5minutes_not_passed)
{
//do staff
if(solution==true)
break;
}
}

但我不知道在 5minutes_not_passed 中该放什么.

But I dont know what to put in the 5minutes_not_passed.

函数声明如下:

void func1(array<array<int,4>,4> initial)
void func2(array<array<int,4>,4> initial)

我发现我可以使用线程库,但我认为与线程结合起来不是一个好主意.我相信需要像计时器这样的东西.请注意,该功能有时可能会在 5 分钟前结束.

I have found that I can use the thread library but I dont think meshing up with threads in a good idea. I believe something like a timer is needed. Note that the functions sometimes might end before 5 minutes.

推荐答案

使用 time_ttime 获取当前秒数.

Use time_t and time to get the current second.

将 5 * 60 添加到该值.

add 5 * 60 to that value.

在循环的每次迭代中,获取时间,如果它大于或等于您的限制,则突破

in every iteration of the loop, get the time, and if it is greater than or equal to your limit, break out

这篇关于如果它们没有结束运行,则在 5 分钟内停止功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆