循环执行一个函数,定时退出,不能在函数外部添加代码,应该怎么做?

2020-09-16 18:58:54 +08:00
 ReputationZh

可以将题目理解成这样: while(1) { //不允许添加代码 Fun(); //不允许添加代码 }

要求函数执行 15s 后退出。 函数执行速度很快,可能会在 1us~10us 左右。

我在函数内定义 static clock_t,但是发现 clock 遇到阻塞后不会计算时间,于是我放弃了这个方案。 后来使用 time_t,发现 time_t 的精度为秒,也放弃了。

大佬们救救孩子吧。

2167 次点击
所在节点    Linux
11 条回复
anytk
2020-09-16 20:53:19 +08:00
clock_gettime
Chowe
2020-09-16 23:39:35 +08:00
休眠算吗?
l12ab
2020-09-16 23:56:48 +08:00
换个思路,你这就是每隔 15 秒执行一次,所以定时触发
nvkou
2020-09-17 00:01:53 +08:00
看起来 fun 是无返回的啊。多线程处理?守护线程定时发起任务和终止任务。
nightwitch
2020-09-17 00:04:05 +08:00
https://www.man7.org/linux/man-pages/man2/timer_create.2.html

创建一个 timer,在 while 的前面设置 timer 的时间。时间到了会给进程发 signal,你就从函数里面出来了。
nightwitch
2020-09-17 00:08:49 +08:00
https://www.man7.org/linux/man-pages/man3/ualarm.3.html
如果 us 级的精度够用的话用 ualarm 的 api 会简单点
yazoox
2020-09-17 08:24:39 +08:00
@nightwitch 兄弟,你的意思是,类似下面这样的么?
function A() {

// create a timer, it will be triggered 15s later, and then exit function A such as `return;`, etc.

while(1) {
fun()
}

}
iceheart
2020-09-17 08:46:57 +08:00
clock_gettime
gettimeofday
nightwitch
2020-09-17 10:49:44 +08:00
@yazoox 进程收到信号以后会直接从正在执行的函数里面跳到信号处理函数,大概类似这样吧。

volatile bool flag = True // volatile is necessary
void sighandler(int signum) {
flag=False;
}

// set a timer, flag will be false when timer expires
while(flag)
{
func(); //func shoule be async-signal-safe
}
ReputationZh
2020-09-17 13:06:07 +08:00
```c
int fun()
{
int ret = -1;

static uint64_t RunTime = 0;
struct timeval tvBegin = {0, 0};
struct timeval tvEnd = {0, 0};

do
{
gettimeofday(&tvBegin, NULL);
if (RunTime > 50000000)
{
dprint("Timeout.");
RunTime = 0;
ret = 0;
break;
}
usleep(1);
gettimeofday(&tvEnd, NULL);
RunTime += (tvEnd.tv_sec * 10000000 + tvEnd.tv_usec) - (tvBegin.tv_sec * 10000000 + tvBegin.tv_usec);
} while (0);

return ret;
}

int main(int argc, char const *argv[])
{
int ret = -1;
while (ret != 0)
{
ret = fun();
}
return 0;
}
```

目前我是这么做的…
TomVista
2020-09-17 14:39:04 +08:00
新开一个线程跑这个 while(1),到点了,杀了

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/707662

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX