gpt4 book ai didi

c - 根据时间执行函数

转载 作者:行者123 更新时间:2023-11-30 16:48:00 26 4
gpt4 key购买 nike

我正在尝试创建一种方法,让execute_func函数每分钟执行一次。

我尝试在几分钟内循环,但是该函数从未在函数下方执行

void main(void) {
time_t timer;
struct tm* tmr;

timer = time(NULL);
tmr = localtime(&timer);

if (tmr->tm_min == 1)
execute_func();
}
return;
}

最佳答案

在 Linux 系统上,系统调用就可以做到这一点。下面是每隔一分钟执行一段代码或调用一个函数的示例:

while(true) 
{
system("sleep 60s");
computeAFunction(); /* The function that you want to execute after every one minute */

if( (true ) /* some code that you want to execute */
{
exit(0); /* if true exit */

}
else
{
continue; /* or else keep running */
}

}

我个人使用过这个,当我想继续计算一个随着时间的推移而变化的特定值时,所以我必须对新计算的值做出决定。您可以根据自己的需要对其进行一些修改。

或者,您可以在 Windows 系统上使用 windows.h 头文件中定义的 sleep() 函数,在 Linux 上使用 unistd.h 中定义的 sleep() 函数。

sleep(1000) 等于 sleep 1 秒。

关于c - 根据时间执行函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43148664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com